Simple Interest program

Program Description

The program demonstrates the use of classes in C++ using simple interest program as an example."SimpleInterest" class holds the various parameters and functions required to calculate interest.

Program
/*********************************
* code.cheraus.com
* 
* Simple Interest program
*
*********************************/

#include<iostream>

using namespace std;

//class definition
class SimpleInterest{
public:
        float p,r,t;

        //constructor for initialization		
        SimpleInterest()
		{ 
		  p=r=t=0;   //making all values 0 during initialization
		}
		
		
		//function declarations
        void getdata();
        void display();

};

/*
Function definition for getdata() function

This function takes the necessary data from the user for the
"SimpleInterest" class.

*/

void SimpleInterest::getdata()
{
   cout<<"\nEnter the Initial Amt : ";
   cin>>p;
   cout<<"\nEnter the Rate of Interest : ";
   cin>>r;
   cout<<"\nEnter the time period : ";
   cin>>t;
}


/*
Function definition for display() function

This function calculates and displays the simple interest for the associated object.

*/
void SimpleInterest::display()
{
  float ans = 0;
  ans = (p*r*t)/100;
  cout<<"\nThe Simple Interest is : "<<ans<<"\n";
}

 
//start of main function 
int main()
{
   //object declaration
   SimpleInterest si;  
   
   //take user data
   si.getdata();
   
   //calculate and display simple interest
   si.display();
   return 0;
}

Output
Test Case 1:

Enter the Initial Amt : 1200
Enter the Rate of Interest : 4.3
Enter the time period : 2
The Simple Interest is : 103.2

Test Case 2:

Enter the Initial Amt : 123
Enter the Rate of Interest : 23
Enter the time period : 1.23
The Simple Interest is : 34.7967

Comments
comments powered by Disqus