Friday, October 19, 2012

Java program to calculate Simple Interest - homework exercise

How to calculate simple interest in Java or Write a Java program to calculate Simple interest is a popular Java programming exercise people use in Java course in school and college. So if you have recently got an homework exercise about calculating Simple interest in Java and wondering How to write Java program than this tutorial can help you, provided you know What is simple interest and What is formula of simple interest. If you are completely new in Java programming than I would suggest to try HelloWorld Example in Java and understand What is Path and Classpath in Java. IF you have been doing Java programming and looking for some good coding exercise than you can also see our earlier programs e.g. calculating Fibonacci series using recursion, checking if number is palindrome and How to reverse String in Java using recursion.

How to calculate Simple Interest in Java

Write a Java program to calculate Simple InterestHere is full code example of Java program to calculate Simple Interest in Java. This is an interactive Java program which accept Input from User using java.util.Scanner class. By the way simpleInterest() method is reusable method and I have made it static so that I can call it from main method in Java without creating object of this class. Its good Java practice to make utility method static.

/**
 *
 * Java program to calculate Simple Interest in Java.
 * Input to program is principle, rate and time and output is simple interest
 * @author
 */

public class SimpleInterestTest{

 
    public static void main(String args[]) {
     
        //creating scanner to accept principle, rate and time input form user
        Scanner scanner = new Scanner(System.in);
        System.out.println("Welcome in Java program to calculate Simple interest");
     
        System.err.println("Please enter principle amount :");
        float amount = scanner.nextFloat();
     
        System.err.println("Enter time in years : ");
        float time = scanner.nextFloat();
     
        System.out.println("Enter rate annually : ");
        float rate = scanner.nextFloat();
     
        float interest = simpleInterest(amount, rate, time);
     
        System.out.println("Simple interested calculate by program is : " + interest);
    }
 
    public static float simpleInterest(float principle, float rate, float time){
        float interest = (principle*rate*time)/100;
        return interest;
    }
}

Output:
Welcome in Java program to calculate Simple interest
Please enter principle amount :
1000
Enter time in years :
1
Enter rate annually :
7
Simple interested calculate by program is : 70.0


So we saw we have create a static method to calculate simple interest for given amount, rate and time. By the way this Java program is not validating input e.g. time must be greater than zero, rate should be more than zero, amount can not be zero or negative, which will be require if you are writing production code.

Some more Java Coding Exercise for Programmer

4 comments:

  1. Nice one. Thanks for sharing your knowledge regarding the formula for calculating simple interest .

    ReplyDelete
  2. i have one doubt, this output amount is per month rate or per year rate.

    ReplyDelete
  3. per year rate not per month...

    ReplyDelete

Java67 Headline Animator