The program shows how to Calculate Simple Interest, Amount, and Compound Interest using a Switch in java. The program uses a scanner class to take the inputs from the user.
The switch case is mostly used when it is necessary to give options to the users. The following program gives users 3 different options to choose from and it displays results accordingly.
If you do not know the working process of the switch case statement then click the link below.
Java Program to Calculate Simple Interest, Amount and Compound Interest
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | //calculate Simple Interest, Amount in java //and Compound Interest using switch import java.util.Scanner; import java.io.*; class InterestAmountCI { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Press 1 to find Simple Interest"); System.out.println("Press 2 to find the Amount"); System.out.println("Press 3 to find the Compound Interest"); System.out.print("Enter your Choice: "); int ch = sc.nextInt(); switch(ch) { case 1: System.out.println("Enter Principal, Rate and Time:"); float principal = sc.nextFloat(); float rate = sc.nextFloat(); float time = sc.nextFloat(); float SI =principal*rate*time/100; System.out.println("Simple Interest: "+SI); break; case 2: System.out.println("Enter principal and Interest:"); float P = sc.nextFloat(); float I = sc.nextFloat(); float Amount =P + I; System.out.println("Amount: "+Amount); break; case 3: System.out.println("Enter principal, Rate and Time:"); float Pr = sc.nextFloat(); float r = sc.nextFloat(); float t = sc.nextFloat(); double CI = Pr * Math.pow( 1.0 + r, t); System.out.println("Compound Interest: "+CI); break; default:System.out.println("Invalid Input"); break; } } } |
Output:
1 2 3 4 5 6 7 8 9 | Press 1 to find Simple Interest Press 2 to find the Amount Press 3 to find the Compound Interest Enter your Choice: 1 Enter Principal, Rate and Time: 1000 5 2 Simple Interest: 100.0 |