Java Programming Examples Tutorial Index

Java Number Programs

This Java program is used to calculate the simple and compound interest for the given values of amount, rate and time.



Example:

import java.util .*;
class sici
{
    public static void main (String argu[ ])
{
    double pr, rate, t, sim,com;
    Scanner sc=new Scanner (System. in);
    System.out.println("Enter the amount:");
    pr=sc.nextDouble();
    System. out. println("Enter the No.of years:");
    t=sc.nextDouble();
    System. out. println("Enter the Rate of  interest");
    rate=sc.nextDouble();
    sim=(pr * t * rate)/100;
    com=pr * Math.pow(1.0+rate/100.0,t) - pr;
    System.out.println("Simple Interest="+sim);
    System.out. println("Compound Interest="+com);
   }
}

Explanation:

First you have to import the util package of Java so that you can use the Scanner class in this program which will help programmers to fetch input from users. Then define a class name sici. Inside the class define the main() function. All Java program needs one main() function from where it starts executing program.

Next declare a double type variable name pr, rate, t, sim and com. Also define an object name s of Scanner class using which the value will be fetched from input device. Then System.out.println() method will be used to display a message - "Enter the amount:". Then the statement pr=sc.nextDouble(); will be used to fetch the value from keyboard, and parse it to integer from string and store to variable pr. Similarly the values for time and rate will also be fetched from the user using sc.nextDouble().

Now you have to use the formula for simple interest to calculate the SI with the given values. So the statement will be: (pr * t * rate)/100; which will get assigned to variable sim. Similarly the formula for compound interest is: pr * Math.pow(1.0+rate/100.0,t) - pr; and its calculated value will be assigned to variable com. The next two System.out.println() methods will be used to display the calculated result of simple and compound interest.



Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram