How to do exponentiation in Java?

6

I'm trying to do a compound interest exercise and I have this code that follows so far.

public class ExDesafio_Aula1 {
        public static void main(String[]args){
            double investimento = 5000.00;
            double juros = 0.01;

            double valorFinal = investimento * (1 + juros) * 12;
            System.out.println(valorFinal);
    }
}

The problem is that at the end of the account, it should raise the value in parentheses by 12, and not multiply it.

How do I solve this problem?

    
asked by anonymous 23.08.2016 / 22:59

1 answer

7

Try using Math.pow :

public class ExDesafio_Aula1 {
    public static void main(String[] args) {
        double investimento = 5000.00;
        double juros = 0.01;

        double valorFinal = investimento * Math.pow(1 + juros, 12);
        System.out.println(valorFinal);
    }
}

See here working on ideone.

    
23.08.2016 / 23:02