Thursday, September 14, 2023

How to calculate Simple Interest in Java Program? Example

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 courses in school and college. So if you have recently got a homework exercise about calculating Simple interest in Java and wondering How to write a Java program then this tutorial can help you, provided you know What is simple interest and What is the formula of simple interest. If you are completely new to Java programming then I would suggest trying 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 exercises then you can also see our earlier programs like calculating the Fibonacci series using recursion, checking if the number is a palindrome, and How to reverse String in Java using recursion.

And, if you need a tough one then I suggest you to write a program to calculate EMI for a 2 million $ loan for 25% and 4% fixed rate interest rate. Also create a full amortization schedule, I mean how much EMI you will pay every month until loan in fully paid. 

How to calculate Simple Interest in Java? Example

Here is a full code example of a Java program to calculate Simple Interest in Java. This is an interactive Java program which accept Input from User using java.util.Scanner class.

How to calculate Simple Interest in Java Program? Example


By the way, simpleInterest() method is a reusable method and I have made it static so that I can call it from the 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

Also, what is your favorite Java programming exercise? Palindrom, Prime Number, Fibonacci series, Binary Search, or this one?  Do let me know in comments. 

17 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
  4. That's interesting post.

    ReplyDelete
  5. Can you please post a Java program to calculate compound interest as well, please make interest is compounded quarterly or half yearly or yearly.

    thanks

    ReplyDelete
    Replies
    1. @Anonymous, sure we'll post program to calculate compound interest in Java shortly.

      Delete
    2. import java.util.Scanner;

      public class CurrentWorthCalculator
      {
      public static void main(String[] args)
      {
      double a=0;
      double r=0;
      double n=0;
      double x=0;

      Scanner input ;


      System.out.println("Welcome in Java program to calculate Present Value");
      input = new Scanner(System.in);

      System.out.print("Please enter the future Value:");
      a= input.nextDouble();


      System.out.print("Please enter the rate of inflation/interest(in %):");
      r= input.nextDouble();

      System.out.println("Please enter the time period (in years)");
      n= input.nextDouble();

      x= a / Math.pow((1 + r/100),n);

      System.out.println("Present Value is : " + x);

      }
      }


      To calculate reverse compound int/ present value

      Delete
    3. hello @Anonymous, does the present value is equal to compound interest, don't you need to subtract principle to print compound interest?

      Delete
  6. nice ones but can u make a program for simple interest with a fixed numericals

    ReplyDelete
  7. I want know that can we do this programme using object oriented programming method?

    ReplyDelete
  8. can we do this program by comparing simple Interest of two different banks using packages?

    ReplyDelete
  9. Please provide validations like principal amount is not greater than 100000 and rate of interest is not greater than 10℅

    ReplyDelete
    Replies
    1. That's actually good enhancements and you can do that by creating a method which reads user input and validate before calculating simple interest.

      Delete
  10. when i run the programm i getting scaner errore what can i do

    ReplyDelete
    Replies
    1. Hello Unknown, can you post your code and error? It may be possible what you are entering wrong input and code is not able to parse it.

      Delete

Feel free to comment, ask questions if you have any doubt.