How to calculate Simple Interest in Java Program? Example (original) (raw)

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.