Java program to calculate compound Amount and Principle Amount using Compound Interest

Hello Guys,
Now I am presenting a java program to calculate compounded amount and principle amount with certain rate of interest and number of years:-
 
public static void calculateCompoundInterest(){
        float total = 1.0f;
        float compoundedAmount = 0.0f;
       
        Scanner sc = new Scanner(System.in);
        System.out.println("enter the principle amount");
        float pa = sc.nextInt();
        System.out.println("enter the rate of interest");
        float ri = sc.nextInt();
        System.out.println("enter no of years");
        int time = sc.nextInt();
       
        //formula of compound interest
        // amount = principle*(1+rate/100)^time
       
        float amount = (1+ri/100);
       
        if(time !=1){
           
            int count = 1;
            while(count<=time){
                total = total * amount;
                count++;
            }
           
            compoundedAmount = total*pa;
           
            System.out.println("Your compounded amount is: "+compoundedAmount);
        }else{
            compoundedAmount = amount*pa;
           
            System.out.println("Your compounded amount is: "+compoundedAmount);
           
        }
       
    }
   
    public static void calculatePrincipleAmount(){
       
        float total = 1.0f;
        float principle = 0.0f;
       
        Scanner sc = new Scanner(System.in);
        System.out.println("enter the compounded amount");
        float ca = sc.nextInt();
        System.out.println("enter the rate of interest");
        float ri = sc.nextInt();
        System.out.println("enter no of years");
        int time = sc.nextInt();
       
        //formula of compound interest
        // principle = amount/(1+rate/100)^time
       
        float amount = (1+ri/100);
       
        if(time !=1){
           
            int count = 1;
            while(count<=time){
                total = total * amount;
                count++;
            }
           
            principle = ca/total;
           
            System.out.println("Your principle amount is: "+principle);
        }else{
            principle = ca/amount;
           
            System.out.println("Your principle amount is: "+principle);
           
        }
       
    }
   
    public static void main(String[] args) {
        System.out.println("Press 1 If you want to calculate the whole amout using compound interest.");
        System.out.println("Press 2 If you want to calculate the principle amount for a certain amount.");
       
        Scanner sc = new Scanner(System.in);
        int choice = sc.nextInt();
       
        if(choice == 1)
            calculateCompoundInterest();
        else
            calculatePrincipleAmount();
    }

}

Comments