Nandakumar Edamana
Share on:
@ R t f

How to Write a Simple Interest Calculator in Java


The following example shows how to write a basic simple interest calculator in Java. It is a console application, which means it runs in a command line terminal (just like any basic Java application without GUI).

Here we use Scanner class to create a Scanner object (named sc) and call its method nextDouble() to get the user input as a double value. All other calculations are self-explanatory.

import java.util.Scanner; public class SimpleInterestCalc { public static void main (String args[]) { double p, n, r; /* Inputs - principal amount, years, rate */ double i, a; /* Interest and total amount */ Scanner sc = new Scanner(System.in); System.out.println("Enter the principal amount, years and rate (%): "); p = sc.nextDouble(); n = sc.nextDouble(); r = sc.nextDouble(); i = p * n * r/100; a = p + i; System.out.println("The interest is " + i + " and the total amount is " + a); } }

Sample output:

Enter the principal amount, years and rate (%): 2000 2 8 The interest is 320.0 and the total amount is 2320.0

Keywords (click to browse): simple-interest simple-interest-calculator simple-interest-code console-application java oop examples java-programming programming development