Read the principal, annual rate and years, and print the simple interest, compound interest (compounded yearly) and the final amounts, formatted to two decimal places.
Solution
Main.java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
double principal = in.nextDouble();
double rate = in.nextDouble();
int years = in.nextInt();
double simple = principal * rate * years / 100;
double amount = principal * Math.pow(1 + rate / 100, years);
double compound = amount - principal;
System.out.printf("Simple interest: %,.2f (total %,.2f)%n", simple, principal + simple);
System.out.printf("Compound interest: %,.2f (total %,.2f)%n", compound, amount);
System.out.printf("Difference: %,.2f%n", compound - simple);
}
}
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!
We use Google Analytics cookies to see how LearnFrenzy is used and make it better, and Google AdSense to keep it free.
No analytics or ad cookies are set until you accept. Privacy Policy
·
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!