Sunday 15 June 2014

C++ program to calculate Simple interest and Compound interest

Here are C++ programs to calculate Simple interest and Compound interest. First program calculates simple interest and second program calculates compound interest, third and fourth program calculates simple interest making use of default argument and function overloading and fifth program use class to compute both Simple interest and Compound interest.


C++ program to calculate Simple interest

#include<iostream>
using namespace std;
int main()
{
    float principal,rate,result;
    int time;
    cout<<"Enter Principal amount ";
    cin>>principal;
    cout<<"Enter Rate per year in % ";
    cin>>rate;
    cout<<"Enter Time ";
    cin>>time;
    result=principal*(rate/100)*(float)time;
    cout<<"Total interest ="<<result;
    result=result+ principal;
    cout<<"\nTotal amount="<<result;
    return 0;
}

OUTPUT




C++ program to calculate Compound interest

#include<iostream>
#include<math.h>
using namespace std;
int main()
{
    float principal,rate,totalCI;
    int years,n;
    cout<<"Enter principal amount (the initial amount you borrow or deposit) ";
    cin>>principal;
    cout<<"Enter annual rate of interest (as a decimal)";
    cin>>rate;
    cout<<"Enter number of years the amount is deposited or borrowed for. ";
    cin>>years;
    cout<<"Enter number of times the interest is compounded per year ";
    cin>>n;
    totalCI=principal*pow((1+rate/n),n*years);
    cout<<"Total compound interest"<<totalCI;
    return 0;
}

OUTPUT




C++ program to calculate simple interest amount making use of default arguments

#include<iostream>
using namespace std;
void amt(float prin=5000,float rate=.05,int time=5)
{
    cout<<"Principal amount : "<<prin<<"  ";
    cout<<"Rate : "<<rate<<"  ";
    cout<<"Time : "<<time<<"  ";
    cout<<"Total interest : "<<prin*rate*time<<endl;
}
int main()
{
    amt();
    amt(8000.0F);
    amt(9000.0F,.08F);
    amt(9000.0F,.09F,8);
    return 0;
}

OUTPUT




C++ program to calculate simple interest amount using function overloading

#include<iostream>
using namespace std;
void amt(float prin,float rate,int time)
{
    cout<<"Principal amount : "<<prin<<"  ";
    cout<<"Rate : "<<rate<<"  ";
    cout<<"Time : "<<time<<"  ";
    cout<<"Total interest : "<<prin*rate*time<<endl;
}
void amt(float prin,int time)
{
    amt(prin,.05F,time);
}
void amt(float prin,float rate)
{
    amt(prin,rate,5);
}
void amt(int time,float rate)
{
    amt(5000.0F,rate,time);
}
void amt(float rate)
{
    amt(5000.0F,rate,5);
}
void amt(int time)
{
    amt(5000.0F,.05,time);
}
int main()
{
    amt(.09F);
    amt(2);
    amt(9000.0F,8);
    amt(7,.02);
    amt(2000.0F,.03F,6);
    return 0;
}

OUTPUT




C++ program to calculate simple interest amount using class

#include<iostream>
#include<math.h>
using namespace std;
class simint
{
private:
    float principal,rate,tint,tamt;
    int time;
public:
    void getdata()
    {
        cout<<"Enter Principal amount : ";
        cin>>principal;
        cout<<"Enter Rate per year in % : ";
        cin>>rate;
        cout<<"Enter Time in years : ";
        cin>>time;
    }
    void calint()
    {
        tint=principal*(rate/100)*(float)time;
        cout<<"Total interest = "<<tint;
        tamt=tint+principal;
        cout<<"\nTotal amount= "<<tamt;
    }
};
class comint
{
private:
    float principal,rate,totalCI;
    int years,n;
public:
    void getdata()
    {
        cout<<"Enter principal amount (the initial amount you borrow or deposit) : ";
        cin>>principal;
        cout<<"Enter annual rate of interest (as a decimal)";
        cin>>rate;
        cout<<"Enter number of years the amount is deposited or borrowed for : ";
        cin>>years;
        cout<<"Enter number of times the interest is compounded per year : ";
        cin>>n;
    }
    void calint()
    {
        totalCI=principal*pow((1+rate/n),n*years);
        cout<<"Total compound interest : "<<totalCI;
    }
};
int main()
{
    int ch;
    cout<<"***MENU***\n1.) Calculate Simple Interest\n2.) Calculate Compound Interest\nEnter your choice ";
    cin>>ch;
    switch(ch)
    {
    case 1 :
        simint simple;
        simple.getdata();
        simple.calint();
        break;
    case 2 :
        comint compound;
        compound.getdata();
        compound.calint();
        break;
    default :
        cout<<"Sorry wrong choice ";
    }
    return 0;
}


OUTPUT


No comments:

Post a Comment