Write a program to calculate compound interest

Problem statement:- Program to calculate compound interest.

Data requirement:-

   Input Data:-principle,rate,n,time

  Output Data:-
amount

Program in C

Here is the source code of the C Program to calculate compound interest.

Code:

#include<stdio.h>
#include<math.h>
int main()
{
    double amount,principle,rate,n,time;

    printf("Enter principle,rate(%),n and time(years):");
    scanf("%lf %lf %lf %lf",&principle,&rate,&n,&time);

    amount=principle*pow(1+(rate/100)/n,n*time);

    printf("The compound interest is %0.2lf",amount);
}

Input/Output:
Enter principle,rate(),n and time(years):
5000
6
2
3
The compound interest is 5970.26

Program in C++

Here is the source code of the C++ Program to calculate compound interest.

Code:

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    double amount,principle,rate,n,time;

    cout<<"Enter principle,rate(%),n and time(years):";
    cin>>principle>>rate>>n>>time;

    amount=principle*pow(1+(rate/100)/n,n*time);

    cout<<"The compound interest is "<<amount;
}

Input/Output:
Enter principle,rate(%),n and time(years):
7000
4
1
3
The compound interest is 7874.05

Program in Java

Here is the source code of the Java Program to calculate compound interest.

Code:

import java.util.Scanner;
public class CompundInterest {

public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
double amount,principle,rate,n,time;

    System.out.println("Enter principle,rate(%),n and time(years):");
    principle=cs.nextDouble();
    rate=cs.nextDouble();
    n=cs.nextDouble();
    time=cs.nextDouble();

    amount=principle*Math.pow(1+(rate/100)/n,n*time);

    System.out.println("The compound interest is "+amount);
cs.close();
}
}

Input/Output:
Enter principle,rate(%),n and time(years):
8000
2
2
4
The compound interest is 8662.853645024641

Program in Python

Here is the source code of the Python Program to calculate compound interest.

Code:

principle=float(input("Enter principle:"))
rate=float(input("Enter rate(%):"))
n=float(input("Enter n:"))
time=float(input("Enter time:"))
amount=principle*pow(1+(rate/100.0)/n,n*time)
print("The compound interest is",amount)

Input/Output:
Enter principle:90000
Enter rate(%):7.2
Enter n:2
Enter time:7
The compound interest is 147665.5152662109

Post a Comment

0 Comments