Program to accept principal amount, rate of interest, no. of years from the user and display the simple interest

This is an interactive Java program which accept Input from User using java.util.Scanner class. By the way calSI() method is used to calculate the simple interest. The formula to calculate Simple Interest is given below:
simple_interest = (total_amount * rate * no_of_years) / 100

PROGRAM
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

Popular posts from this blog

Program to define a class 'employee' with data members as empid, name and salary. Accept data for 5 objects using Array of objects and print it.

Define a class Student with four data members such as name, roll no.,sub1, and sub2. Define appropriate methods to initialize and display the values of data members. Also calculate total marks and percentage scored by student.

Program to input age from user and throw user-defined exception if entered age is negative