Java Program to Calculate Simple Interest

Write a Java program to accept principal amount, rate of interest, no. of years from the user and display the simple interest.



Source Code
import java.lang.*;	// Import Language package
import java.util.*;	// Import Utility package to accept data

class SimpleInterest {

	int amount;		// data member declaration
	float rate;
	float years;
	double SI;
	
	SimpleInterest(int a, float r, float y) // Constructor to initialize data members
	{
		amount = a;
		rate = r;
		years = y;
	}
	
	void calSI()	// method to calculate simple interest
	{
		
		SI = (amount*rate*years)/100;
		System.out.println("Simple Interest = "+SI);
	}

	public static void main(String args[])
	{
		Scanner s = new Scanner(System.in);
		System.out.print("Enter Principal Amount :: ");
		int amt = s.nextInt();	// Converting string to int
		System.out.print("Enter Rate of Interest :: ");
		float rt = s.nextFloat();
		System.out.print("Enter No. of years :: ");
		float yrs = s.nextFloat();
		SimpleInterest e = new SimpleInterest(amt,rt,yrs);
		e.calSI();
	}
}
Output
C:\>javac SimpleInterest.java
C:\>java SimpleInterest
Enter Principal Amount :: 3000
Enter Rate of Interest :: 5
Enter No. of years :: 4
Simple Interest = 600.0





"Coding Hub - Learn to code" app now available on Google Play Store