English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Neste exemplo, vamos aprender a calcular juros simples e compostos em Java.
Para entender este exemplo, você deve entender o seguinteProgramação JavaTema:
import java.util.Scanner; class Main { public static void main(String[] args) { //Criar um objeto da classe Scanner Scanner input = new Scanner(System.in); //Aceitar a entrada do usuário System.out.print("Digite o capital: "); double principal = input.nextDouble(); System.out.print("Digite a taxa de juros: "); double taxa = input.nextDouble(); taxa = taxa/100; System.out.print("Digite o tempo: "); double tempo = input.nextDouble(); double juros = (principal * tempo * taxa) / 100; System.out.println("Principal: ", + principal); System.out.println("Interest Rate: ", + rate); System.out.println("Time: ", + time); System.out.println("Juros simples: ", + interest); input.close(); } }
Output Result
Enter the principal: 1000 Enter the interest rate: 8 Enter the time: 2 Principal: 1000.0 Interest Rate: 8.0 Time: 2.0 Juros simples: 160.0
No exemplo acima, usamos a classe Scanner para receber a entrada do usuário. principal,rate e tempoEm seguida, usamos a fórmula da taxa de juros simples para calcular o juros simples.
Juros simples = (Principal * Taxa * Tempo) / 100
import java.util.Scanner; class Main { public static void main(String[] args) { //Criar um objeto da classe Scanner Scanner input = new Scanner(System.in); //Aceitar a entrada do usuário System.out.print("Digite o capital: "); double principal = input.nextDouble(); System.out.print("Digite a taxa de juros: "); double taxa = input.nextDouble(); System.out.print("Digite o tempo: "); double tempo = input.nextDouble(); System.out.print("Digite o número de vezes de juros compostos: "); int número = input.nextInt(); double interesse = principal * (Math.pow((1 + rate/100), ", (time * number))) - principal; System.out.println("Principal: ", + principal); System.out.println("Interest Rate: ", + rate); System.out.println("Time: ", + time); System.out.println("Number of Compounding Times: ", + number); System.out.println("Compound Interest: ", + interest); input.close(); } }
Output Result
Enter the principal: 1000 Enter the interest rate: 10 Enter the time: 3 Enter the number of compounding times: 1 Principal: 1000.0 Interest Rate: 10.0 Time: 3.0 Number of Compounding Times: 1 Compound Interest: 331.00000000000045
In the above example, we used the compound interest formula to calculate compound interest.
Here, we usedMath.pow()Method to calculate the power of a number.