Friday, December 23, 2011

Program to calculate simple and compound interest


import java.util.*;
public class ch2q19
{
    public static void main(String[] args)
    {double rate, sum = 0;
     int n;
     Scanner kbd = new Scanner(System.in);
     System.out.print("Enter interest rate and no of years: ");
     rate = kbd.nextDouble(); n = kbd.nextInt();
     for(int i = 1; i <= n; i++)
      sum += Math.pow(1+rate/100, i);
     System.out.print("What do you want? Enter 1 for Final amount 2 for Annual deposit: ");
     int choice = kbd.nextInt();
     if(choice == 1)
       {System.out.print("Enter annual deposit: ");
        double amt = kbd.nextDouble();
        System.out.printf("Final amount is %.2f\n", amt*sum);
       }
     else {System.out.print("Enter final amount: ");
        double famt = kbd.nextDouble();
        System.out.printf("Annual Deposit is %.2f\n", famt/sum);
       }
     
    }
}

No comments:

Post a Comment