To calculate the simple interest in Java. We need the principal amount, interest per year, and time period in a year from the user.
We can calculate the simple interest by using the formula,
Simple Interest (SI) = (P * R * T )/100
where,
For reading a float number from the user, we can use the nextFloat()
function in the java Scanner class. For reading the float value, we have to write the code as follows:
System.out.print("Enter the Principal amount : ");
p = sc.nextFloat();
First, we have to declare the class SimpleInterest.Then open the main function. Then declare the variables p, r, t, si as float
. Read the principal amount from the user into the variable 'p'. Read the rate of interest per annum in the variable 'r'. Read the time period in a year from the user into the variable 't'. Then calculate simple interest as 'si = p * n * r / 100'.Then display si using the system.out.println()
function.
STEP 1: Declare the class SimpleInterest with a public modifier.
STEP 2: Open the main()
to start the program, Java program execution starts with the main()
STEP 3: Declare the variables p, r, t, si as float.
STEP 4: Read the principal amount from the user into the variable p.
STEP 5: Read the rate of interest in the variable r.
STEP 6: Read the time period in the variable t.
STEP 7: Calculate si=p*i*t/100.
STEP 8: Display simple interest as si.
For calculating the SI, we are using the below java concepts, we recommend you to learn those to have a better understanding
import java.util.Scanner;
public class SimpleInterest
{
public static void main(String args[])
{
float p, r, t,si;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the Principal amount : ");
p = sc.nextFloat();//Read Principla amount
System.out.print("Enter the Rate of interest per year : ");
r = sc.nextFloat();//Read rate of interset
System.out.print("Enter the Time period in year : ");
t = sc.nextFloat();//Read the time period
sc.close();
si = (p * r * t) / 100;//calculate the simple interest
System.out.print("Simple Interest is: " +si);
}
}
OUTPUT 1 Enter the Principal amount :3000 Enter the Rate of interest per year : 7 Enter the Time period in year : 4 Simple Interest is:840.0 OUTPUT 2 Enter the Principal amount :6000 Enter the Rate of interest per year : 10 Enter the Time period in year : 5 Simple Interest is:3000.0