Write a program that calculates the Simple Interest and Compound Interest. The Principal ,Amount, Rate of Interest and Time are entered through the keyboard. content 2

calculates the Simple Interest and Compound Interest

Simple Interest:

Simple Interest is calculated based on the principal amount, rate of interest, and time period. The formula for calculating simple interest is:

Simple Interest = (Principal * Rate * Time) / 100

Here, the principal represents the initial amount of money, the rate is the percentage of interest per unit of time, and the time represents the duration for which the interest is calculated.

Compound Interest:

Compound Interest takes into account the compounding effect, where the interest is added to the principal amount at regular intervals. The formula for calculating compound interest is:

Amount = Principal * (1 + Rate / 100)^Time

Compound Interest = Amount – Principal

In this formula, the amount represents the final amount after including the interest, and it is calculated by multiplying the principal with the compound factor (1 + rate/100) raised to the power of the time.

Python Program

def calculate_simple_interest(principal, rate, time):
    simple_interest = (principal * rate * time) / 100
    return simple_interest

def calculate_compound_interest(principal, rate, time):
    amount = principal * pow((1 + rate / 100), time)
    compound_interest = amount - principal
    return compound_interest

# Getting input from the user
principal = float(input("Enter the principal amount: "))
rate = float(input("Enter the rate of interest: "))
time = float(input("Enter the time period (in years): "))

# Calculating Simple Interest and Compound Interest
simple_interest = calculate_simple_interest(principal, rate, time)
compound_interest = calculate_compound_interest(principal, rate, time)

# Displaying the results
print("Simple Interest:", simple_interest)
print("Compound Interest:", compound_interest)

In this program, the user is prompted to enter the principal amount, rate of interest, and time period in years. The calculate_simple_interest() function takes the principal, rate, and time as arguments and calculates the simple interest using the formula (principal * rate * time) / 100.

The calculate_compound_interest() function also takes the principal, rate, and time as arguments. It uses the formula amount = principal * (1 + rate / 100) ^ time to calculate the final amount and then subtracts the principal to obtain the compound interest.

C Program

#include <stdio.h>
#include <math.h>

float calculate_simple_interest(float principal, float rate, float time) {
    float simple_interest = (principal * rate * time) / 100;
    return simple_interest;
}

float calculate_compound_interest(float principal, float rate, float time) {
    float amount = principal * pow((1 + rate / 100), time);
    float compound_interest = amount - principal;
    return compound_interest;
}

int main() {
    float principal, rate, time;

    printf("Enter the principal amount: ");
    scanf("%f", &principal);

    printf("Enter the rate of interest: ");
    scanf("%f", &rate);

    printf("Enter the time period (in years): ");
    scanf("%f", &time);

    float simple_interest = calculate_simple_interest(principal, rate, time);
    float compound_interest = calculate_compound_interest(principal, rate, time);

    printf("Simple Interest: %.2f\n", simple_interest);
    printf("Compound Interest: %.2f\n", compound_interest);

    return 0;
}

C++

#include <iostream>
#include <cmath>

float calculate_simple_interest(float principal, float rate, float time) {
    float simple_interest = (principal * rate * time) / 100;
    return simple_interest;
}

float calculate_compound_interest(float principal, float rate, float time) {
    float amount = principal * pow((1 + rate / 100), time);
    float compound_interest = amount - principal;
    return compound_interest;
}

int main() {
    float principal, rate, time;

    std::cout << "Enter the principal amount: ";
    std::cin >> principal;

    std::cout << "Enter the rate of interest: ";
    std::cin >> rate;

    std::cout << "Enter the time period (in years): ";
    std::cin >> time;

    float simple_interest = calculate_simple_interest(principal, rate, time);
    float compound_interest = calculate_compound_interest(principal, rate, time);

    std::cout << "Simple Interest: " << simple_interest << std::endl;
    std::cout << "Compound Interest: " << compound_interest << std::endl;

    return 0;
}

In both the C and C++ programs, the user is prompted to enter the principal amount, rate of interest, and time period in years. The calculate_simple_interest() function takes the principal, rate, and time as arguments and calculates the simple interest using the formula (principal * rate * time) / 100.

The calculate_compound_interest() function also takes the principal, rate, and time as arguments. It uses the formula amount = principal * pow((1 + rate / 100), time) to calculate the final amount and then subtracts the principal to obtain the compound interest.

Java

import java.util.Scanner;

public class InterestCalculator {
    public static double calculateSimpleInterest(double principal, double rate, double time) {
        double simpleInterest = (principal * rate * time) / 100;
        return simpleInterest;
    }

    public static double calculateCompoundInterest(double principal, double rate, double time) {
        double amount = principal * Math.pow((1 + rate / 100), time);
        double compoundInterest = amount - principal;
        return compoundInterest;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the principal amount: ");
        double principal = scanner.nextDouble();

        System.out.print("Enter the rate of interest: ");
        double rate = scanner.nextDouble();

        System.out.print("Enter the time period (in years): ");
        double time = scanner.nextDouble();

        double simpleInterest = calculateSimpleInterest(principal, rate, time);
        double compoundInterest = calculateCompoundInterest(principal, rate, time);

        System.out.println("Simple Interest: " + simpleInterest);
        System.out.println("Compound Interest: " + compoundInterest);
    }
}

javaScript

function calculateSimpleInterest(principal, rate, time) {
    const simpleInterest = (principal * rate * time) / 100;
    return simpleInterest;
}

function calculateCompoundInterest(principal, rate, time) {
    const amount = principal * Math.pow((1 + rate / 100), time);
    const compoundInterest = amount - principal;
    return compoundInterest;
}

const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

rl.question("Enter the principal amount: ", function(principal) {
    rl.question("Enter the rate of interest: ", function(rate) {
        rl.question("Enter the time period (in years): ", function(time) {
            const simpleInterest = calculateSimpleInterest(parseFloat(principal), parseFloat(rate), parseFloat(time));
            const compoundInterest = calculateCompoundInterest(parseFloat(principal), parseFloat(rate), parseFloat(time));

            console.log("Simple Interest: " + simpleInterest);
            console.log("Compound Interest: " + compoundInterest);

            rl.close();
        });
    });
});

In both the Java and JavaScript programs, the user is prompted to enter the principal amount, rate of interest, and time period in years. The calculateSimpleInterest() function takes the principal, rate, and time as arguments and calculates the simple interest using the formula (principal * rate * time) / 100.

The calculateCompoundInterest() function also takes the principal, rate, and time as arguments. It uses the formula amount = principal * Math.pow((1 + rate / 100), time) to calculate the final amount and then subtracts the principal to obtain the compound interest.

Scroll to Top