Computer Applications

The Simple Interest (SI) and Compound Interest (CI) of a sum (P) for a given time (T) and rate (R) can be calculated as:

(a) SI = (p * r * t) / 100

(b) CI = P * ((1 + (R / 100))T - 1)

Write a program to input sum, rate, time and type of Interest ('S' for Simple Interest and 'C' for Compound Interest). Calculate and display the sum and the interest earned.

Java

Java Conditional Stmts

80 Likes

Answer

import java.util.Scanner;

public class KboatInterest
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter the sum: ");
        double p = in.nextDouble();
        System.out.print("Enter rate: ");
        double r = in.nextDouble();
        System.out.print("Enter time: ");
        int t = in.nextInt();
        System.out.println("Enter type of Interest");
        System.out.print("('S'- Simple Interest 'C'- Compound Interest): ");
        char type = in.next().charAt(0);
        boolean isTypeValid = true;

        double interest = 0.0;

        switch (type) {
            case 'S':
            interest = p * r * t / 100;
            break;

            case 'C':
            interest = p * (Math.pow((1 + (r / 100)), t) - 1);
            break;

            default:
            isTypeValid = false;
            System.out.println("Incorrect Interest type");
            break;
        }

        if (isTypeValid) {
            double amt = p + interest;
            System.out.println("Sum = " + p);
            System.out.println("Interest = " + interest);
            System.out.println("Sum + Interest = " + amt);
        }
    }
}

Variable Description Table

Program Explanation

Output

Answered By

31 Likes


Related Questions