Java program to calculate compound interest code example

Example 1: Java program to find simple interest

// Java program to find simple interest
import java.util.Scanner;
public class SimpleInterestJavaProgram
{
   public static void main(String[] args)
   {
      float principal, rate, time;
      Scanner sc = new Scanner(System.in);
      System.out.print("Enter principal amount : ");
      principal = sc.nextFloat();
      System.out.print("Please enter rate annually : ");
      rate = sc.nextFloat();
      System.out.print("Please enter time(in years) : ");
      time = sc.nextFloat();
      float simpleInterest;
      simpleInterest = (principal * time * rate) / 100;
      System.out.println("The Simple Interest is : " + simpleInterest);
      sc.close();
   }
}

Example 2: Java program to calculate compound interest

// Java program to calculate compound interest
public class CompoundInterestDemo 
{
   public void calculateCompound(int p, int t, double r, int n) 
   {
      double number = p * Math.pow(1 + (r / n), n * t);
      double interest = number - p;
      System.out.println("Compound interest after " + t + " years: " + interest);
      System.out.println("Money after " + t + " years: " + number);
   }
   public static void main(String[] args) 
   {
      CompoundInterestDemo obj = new CompoundInterestDemo();
      obj.calculateCompound(200000, 6, .06, 12);
   }
}

Tags:

Java Example