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 Tiago Prudente 23.08.2016 в 22:59
source

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.

    
answered by 23.08.2016 в 23:02