Thursday, 24 November 2022

Java program to find simple and compound Interest using this keyword.

 

package usageofthis;

 import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

 class Interest{ 

int p; 

float r; 

int n;

double si;

double ci;

double tv;

Interest(int p,float r,int n){ 

this.p=p; 

this.r=r; 

this.n=n;

}

void simpleinterest(int p,float r,int n){

     si=(p * n * r)/100;

     tv= p +si;

}

void compoundinterest(int p1,float r1,int n1){

     ci=p1 * Math.pow(1+r1,n1);

     tv= p +ci;

}

void display1(){

    System.out.println("Principal = "+p);

    System.out.println("Interest Rate=  "+r);

    System.out.println("Time period= "+n);

    System.out.format("Simple Interest %.2f \n", si);

    System.out.format("Total Value %.2f \n ", tv);

    }

void display2(){

    System.out.println("Principal = "+p);

    System.out.println("Interest Rate=  "+r);

    System.out.println("Time period= "+n);

    System.out.format("Compound  Interest %.2f \n", ci);

    System.out.format("Total Value %.2f \n ", tv);

     }

}

public class Usageofthis {

       public static void main(String[] args) throws IOException {

                   BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

                  

                    System.out.println("Enter the Principal amount");

                    int p = Integer.parseInt(br.readLine());

                   

                    System.out.println("Enter the Interest Rate in float");

                    float r = Float.parseFloat(br.readLine());

                   

                    System.out.println("Enter the Time Period in months ");

                    int n = Integer.parseInt(br.readLine());

                    Interest s1=new Interest(p,r,n);

                    System.out.println("Enter your choice ");

                    System.out.println("1.Simple Interest");

                    System.out.println("2.Compound Interest");

                    int choice = Integer.parseInt(br.readLine());

                    

                    switch(choice){

                        case 1:{

                           s1.simpleinterest(p, r, n);

                           s1.display1(); 

                           break;

                        }

                        case 2:{

                           s1.compoundinterest(p, r, n);

                           s1.display2(); 

                           break;

                        }

                        default:    

                        System.out.println("Please select your choice between 1& 2");

          

     }}

OUTPUT 1:












OUTPUT 2:





No comments:

Post a Comment