Java Program to Calculate Simple Interest and Compound Interest
Scanner Class and Operator in Java.
The java.util.Scanner class is used for reading input from various sources like the keyboard, file, or network socket. It provides methods for reading different data types, including strings, primitive types (int, double, etc.), and tokenized input.
On the other hand, Java doesn’t have a class called java.util.Operator. When performing mathematical operations on the input data, the java.util.Scanner class can be used in conjunction with a number of Java operators, such as +, -, *, /, etc.
To know more about Java program to calculate Simple Interest and Compound Interest read the complete article.
Steps for writing a Java program to calculate Simple Interest and Compound Interest:
- Define the variables for principal amount, rate of interest, and time period.
- Calculate simple interest using the formula: (principal * rate * time) / 100.
- Calculate compound interest using the formula: principal * (1 + rate / 100)^time.
- Print the result of both simple and compound interest.
- Declare variables for the principal amount (P), the interest rate (R), and the time period (T).
- Calculate the simple interest using the formula: SI = (P * R * T) / 100
- Display the result..
- Declare variables for the principal amount (P), the interest rate (R), and the time period (T).
- Calculate the compound interest using the formula: CI = P * (1 + (R / 100))^T.
- Display the result.
Let’s look at a Java program to Calculate simple Interest and Compound Interest
Example 1: Java Program to calculate Simple Interest
public class Main { public static void main(String[] args) { System.out.print("Principal = 9000: "); double principal = 9000; System.out.print("rate of interest = 10 : "); double rate = 10; System.out.print("number of years = 5 : "); int years = 5; double simpleInterest = (principal * rate * years) / 100; System.out.println("Simple Interest: " + simpleInterest); } }
Output
Principal = 9000: rate of interest = 10 : number of years = 5 : Simple Interest: 4500.0
- This program takes three inputs: .
- The principal amount, the rate of interest, and the number of years,
- Calculates the simple interest using the formula interest = (principal * rate * years) / 100.
- The result is then printed to the console.
Example 2 :Java Program to calculate Simple Interest and Compound Interest
import java.util.*; public class Main { public static void main(String []args){ double p, r, t, s_interest, c_interest; p = 50000; r = 20; t = 5; System.out.println("Principal = "+p); System. out. println("Annual Rate of Interest = "+r); System. out. println("Time (years) = "+t); s_interest = (p * r * t)/100; c_interest = p * Math.pow(1.0+r/100.0,t) - p; System.out.println("Simple Interest: "+s_interest); System.out. println("Compound Interest: "+c_interest); } }
Output
Principal = 50000.0 Annual Rate of Interest = 20.0 Time (years) = 5.0 Simple Interest: 50000.0 Compound Interest: 74415.99999999997
- It starts by defining some variables: p (the principal amount), r (the annual rate of interest), and t (the number of years). These values are set to 50000, 20, and 5 respectively.
- After that, it calculates the simple interest using the formula s_interest = (p * r * t)/100 and stores it in the s_interest variable.
- The code also calculates the compound interest using the formula c_interest = p * Math.pow(1.0+r/100.0,t) - p and stores it in the c_interest variable.
- Finally, the code prints the values of s_interest and c_interest using the System.out.println method.
Example 3: Java Program to Calculate Simple Interest and Compound Interest
import java.math.BigDecimal; import java.math.MathContext; public class Main { public static void main(String[] args) { double principal = 10000; double rate = 5; double time = 5; // Simple Interest double simpleInterest = (principal * rate * time) / 100; System.out.println("Simple Interest: " + simpleInterest); // Compound Interest BigDecimal compoundInterest = BigDecimal.valueOf(principal).multiply(BigDecimal.valueOf(1 + (rate / 100)).pow((int) time, MathContext.DECIMAL64)); System.out.println("Compound Interest: " + compoundInterest); } }
Output
Simple Interest: 2500.0 Compound Interest: 12762.81562500000
- The Java code below computes simple interest and compound interest. It declares variables for the principal amount, interest rate, and time period and uses the BigDecimal class from the java.math package to perform high precision decimal calculations.
- The formulas for calculating simple interest and compound interest are SI = (P * R * T) / 100 and CI = P * (1 + (R / 100))T, respectively. On the console, the computations' outcomes are shown..
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment