Display the original deposit the interest earned and the new
Display the original deposit, the interest earned and the new Balance (with interested added to the deposit. Your program will prompt user for the name of depositor, amount of deposit, number of times the interest is compounded per year, and years on deposit both as real numbers. Verify all inputted numbers are positive and not character, otherwise stop the program with an error message.
Program =>
#include <iostream>
#include <cmath>
using namespace std;
int main(){
// declaring variables
double principal;
double rate;
double year;
int num_of_times;
double interest;
// taking user input
cout<<\"Enter principal amount: \";
cin>>principal;
cout<<\"Number number of years: \";
cin>>year;
cout<<\"How many number of time the interest is compounded per year :\";
cin>>num_of_times;
// getting rate value based on year value
if(year >= 5)
rate = 4.5;
else if(year >= 4)
rate = 4;
else if(year >= 3)
rate = 3.5;
else if(year >= 2)
rate = 2.5;
else if(year >= 1)
rate = 2;
else
rate = 1.5;
// calculating amount after year
double A= principal*pow(1+rate/(num_of_times*100), year*num_of_times);
interest = A - principal;
cout<<\"Amount of money accumulated after \"<<year<<\" years: $\"<<A<<endl;
cout<<\"Interest: $\"<<interest<<endl;
return 0;
}
Solution
=========================ANSWER=========================================
#include <iostream>
#include <cmath>
using namespace std;
int main(){
// declaring variables
double principal;
double rate;
double year;
int num_of_times;
double interest;
bool valid = true;
string name;
// taking user input
cout<<\"Enter the name of depositor: \";
cin>>name;
cout<<\"Enter principal amount: \";
cin>>principal;
if(cin.fail())
{
valid = false;
}
cout<<\"Number number of years: \";
cin>>year;
if(cin.fail())
{
valid = false;
}
cout<<\"How many number of time the interest is compounded per year :\";
cin>>num_of_times;
if(cin.fail())
{
valid = false;
}
cout<<endl<<\"-------------------------output-----------------------------------------------\"<<endl;
while(valid==false)
{
cout<<\"Program is stop due to incorrect input Parameter \"<<endl;
return 0;
}
// getting rate value based on year value
if(year >= 5)
rate = 4.5;
else if(year >= 4)
rate = 4;
else if(year >= 3)
rate = 3.5;
else if(year >= 2)
rate = 2.5;
else if(year >= 1)
rate = 2;
else
rate = 1.5;
// calculating amount after year
double A= principal*pow(1+rate/(num_of_times*100), year*num_of_times);
interest = A - principal;
cout<<\"original deposit amount: $\"<<principal<<endl;
cout<<\"interest earned: $\"<<interest<<endl;
cout<<\"Amount of money accumulated after \"<<year<<\" years: $\"<<A<<endl;
return 0;
}//end of main
------------------------------------------------------------------------------------------------------------------------------------------------
Example 1:- all inputs are correct
Enter the name of depositor: User
Enter principal amount: 3000
Number number of years: 3
How many number of time the interest is compounded per year :2
-------------------------output-----------------------------------------------
original deposit amount: $3000
interest earned: $329.107
Amount of money accumulated after 3 years: $3329.11
Example 2:- all inputs are not correct
Enter the name of depositor: user
Enter principal amount: 3000
Number number of years: 2
How many number of time the interest is compounded per year :rj
-------------------------output-----------------------------------------------
Program is stop due to incorrect input Parameter
--------------------------------------------------------------------------------------------------------------------
If you have any query, please feel free to ask
Thanks a lot


