This is my java code, I am starting and we are giving the types of variables and how to use them. They ask us for a program with a certain behavior, my question is why profit is 0 if I declare it as a double and interest as an integer? Note that I leave interest as an integer so that values with decimals are not entered
package tema_2_2;
public class Ej_8 {
/*
Si un banco nos paga el 7% de intereses e ingresamos 4000 euros. ¿Cuánto
ganaremos después de un año?. ¿Qué importe final tendremos en el banco al final
del año?
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
double dinero_i = 4000;
int interes = 7;
double ganancia = dinero_i * (interes / 100);
double dinero_f = dinero_i + ganancia;
System.out.println("Hemos ganado " +ganancia+ " euros y ahora tenemos " +dinero_f+ " euros");
}
}
When you divide 2 whole numbers the result will be a whole number and when the numerator is less than the denominator the result will be 0. that's why when you divide interest/100 it gives you 0
If we want it to show us the result in decimals we have to cast either the numerator or denominator