Add at least one conditional expression to your program. Examples: In the progra
ID: 3729080 • Letter: A
Question
Add at least one conditional expression to your program.
Examples: In the programming tutorial, you would add a tutorial on conditional expressions. You can then add simple multiple choice questions to test the user's understanding of the concepts. You might also want to break the tutorial into sections, and use conditional expressions to ask the user which tutorial they wanted to see (i.e., variable declaration, input/output, conditional expressions, etc.).
For the loan calculator, the program might ask the user if he or she wants to solve for monthly payment, loan amount, length of loan, or interest rate. The program would then ask for the required information and solve for the remaining value.
I need help with this course project. It must be done using C++. I am doing a loan calculator program.
Here is my code:
// Course project, financial calculator.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
using namespace std;
int nMonths = 0;
float loanAmount;
float totalPaid = 0.0f;
void process(){
float interestRate;
float monthlyPayment;
cout << "What is the amount of the loan?:$";
cin >> loanAmount; cin.ignore();
cout << "What is the interest rate on the loan?:$";
cin >> interestRate; interestRate /= 100; cin.ignore();
cout << "What is the desired monthly payment?:$";
cin >> monthlyPayment; cin.ignore();
while (monthlyPayment < loanAmount * interestRate / 12) {
cout << "Not a valid amount, Amount must be greater than $";
cout << (loanAmount * interestRate / 12) << endl;
cout << "What is the desired monthly payment amount? $";
cin >> monthlyPayment; cin.ignore();
}
float interestToAdd = 0.0f;
float remaining = loanAmount;
float toAdd = 0.0f;
cout<<endl;
cout << "Month" <<" (Interest Added)"<<" (Amount Paid)"<<" (Debt Remaining)" << endl;
while (remaining > monthlyPayment) {
interestToAdd = remaining * interestRate / 12;
nMonths++;
remaining += interestToAdd;
if (remaining > monthlyPayment) {
toAdd = monthlyPayment;
}
else {
toAdd = remaining;
}
cout << nMonths << " $" << interestToAdd << " $" << toAdd
<< " $" << remaining << endl;
totalPaid += toAdd;
remaining -= toAdd;
}
}
void displayResult(){
cout << endl << "Statistics about loan. " << endl;
cout << "Initial loan amount: $" << loanAmount << endl;
cout << "Total amount paid: $" << totalPaid << endl;
cout << "Time to pay off loan: " << nMonths << endl;
cout << "Overpay percentage: " << (totalPaid / loanAmount) << endl;
cin.ignore();
}
int main()
{
int ch;
do{
cout<<endl<<"************************MENU***************************"<<endl;
cout<<"1. Process Loan"<<endl;
cout<<"2. Display Result"<<endl;
cout<<"Enter your choice : ";
cin>>ch;
switch(ch){
case 1:
process();
break;
case 2:
displayResult();
break;
default:
cout<<" Entered Wrong Keyword.... Re-enter Again";
break;
}
} while(ch != '1' || ch != '2');
return 0;
}
Explanation / Answer
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
using namespace std;
int nMonths = 0;
float loanAmount;
float totalPaid = 0.0f;
void process(){
float interestRate;
float monthlyPayment;
cout << "What is the amount of the loan?:$";
cin >> loanAmount; cin.ignore();
cout << "What is the interest rate on the loan?:$";
cin >> interestRate; interestRate /= 100; cin.ignore();
cout << "What is the desired monthly payment?:$";
cin >> monthlyPayment; cin.ignore();
while (monthlyPayment < loanAmount * interestRate / 12) {
cout << "Not a valid amount, Amount must be greater than $";
cout << (loanAmount * interestRate / 12) << endl;
cout << "What is the desired monthly payment amount? $";
cin >> monthlyPayment; cin.ignore();
}
float interestToAdd = 0.0f;
float remaining = loanAmount;
float toAdd = 0.0f;
cout<<endl;
cout << "Month" <<" (Interest Added)"<<" (Amount Paid)"<<" (Debt Remaining)" << endl;
while (remaining > monthlyPayment) {
interestToAdd = remaining * interestRate / 12;
nMonths++;
remaining += interestToAdd;
//Conditional expression
toAdd = (remaining > monthlyPayment) ? monthlyPayment : remaining;
cout << nMonths << " $" << interestToAdd << " $" << toAdd
<< " $" << remaining << endl;
totalPaid += toAdd;
remaining -= toAdd;
}
}
void displayResult(){
cout << endl << "Statistics about loan. " << endl;
cout << "Initial loan amount: $" << loanAmount << endl;
cout << "Total amount paid: $" << totalPaid << endl;
cout << "Time to pay off loan: " << nMonths << endl;
cout << "Overpay percentage: " << (totalPaid / loanAmount) << endl;
cin.ignore();
}
int main(){
int ch;
do{
cout<<endl<<"************************MENU***************************"<<endl;
cout<<"1. Process Loan"<<endl;
cout<<"2. Display Result"<<endl;
cout<<"Enter your choice : ";
cin>>ch;
switch(ch){
case 1:
process();
break;
case 2:
displayResult();
break;
default:
cout<<" Entered Wrong Keyword.... Re-enter Again";
break;
}
} while(ch != '1' || ch != '2');
return 0;
}
----------
output sample :-
************************MENU***************************
1. Process Loan
2. Display Result
Enter your choice : 1
What is the amount of the loan?:$1000
What is the interest rate on the loan?:$2
What is the desired monthly payment?:$55
Month (Interest Added) (Amount Paid) (Debt Remaining)
1 $1.66667 $55 $1001.67
2 $1.57778 $55 $948.244
3 $1.48874 $55 $894.733
4 $1.39956 $55 $841.133
5 $1.31022 $55 $787.443
6 $1.22074 $55 $733.664
7 $1.13111 $55 $679.795
8 $1.04132 $55 $625.836
9 $0.951394 $55 $571.788
10 $0.861313 $55 $517.649
11 $0.771082 $55 $463.42
12 $0.6807 $55 $409.101
13 $0.590168 $55 $354.691
14 $0.499485 $55 $300.19
15 $0.408651 $55 $245.599
16 $0.317665 $55 $190.917
17 $0.226528 $55 $136.143
18 $0.135239 $55 $81.2784
************************MENU***************************
1. Process Loan
2. Display Result
Enter your choice : 2
Statistics about loan.
Initial loan amount: $1000
Total amount paid: $990
Time to pay off loan: 18
Overpay percentage: 0.99
-------------------------------------------------------------------------------------------------------------------------------
Part 2:-
#include <iostream>
using namespace std;
void vardeclare()
{
cout<<"------variable declaration------"<<endl;
cout<<"Type & Description 1 bool := Stores either value true or false. "<<endl;
cout<<"char := Typically a single octet (one byte). This is an integer type." <<endl;
cout<<"int := The most natural size of integer for the machine."<<endl;
cout<<"float := A single-precision floating point value"<<endl;
cout<<"double :=A double-precision floating point value"<<endl;
cout<<"void := Represents the absence of type."<<endl;
}
void ipop()
{
cout<<"------input/output------"<<endl;
cout<<"standard input stream (cin):"<<endl;
cout<<"standard output stream (cout):"<<endl;
}
void conditionexp()
{
cout<<"------conditional expressions------"<<endl;
cout<<"The basic syntax of using the ternary operator is thus:"<<endl;
cout<<"(condition) ? (if_true) : (if_false)"<<endl;
}
int main()
{
char choice;
cout << "Simple Multiple choice questions to test the user's " << endl<<endl;
cout<<"Which tutorial you wanted to see"<<endl;
cout<<"A. variable declaration"<<endl;
cout<<"b. input/output"<<endl;
cout<<"C. conditional expressions"<<endl;
cout<<"B. exit"<<endl;
cout<<"Enter you Choice : ";
cin>>choice;
switch(choice)
{
case 'A' :
case 'a' : vardeclare();
break;
case 'B' :
case 'b' : ipop();
break;
case 'C' :
case 'c' : conditionexp();
break;
case 'D' :
case 'd': cout<<"you exited !! ";
return 0;
break;
default : cout<<" In correct Choice !"<<endl;
break;
}
return 0;
}
---------------------------------------------------------------------------------------------
If you have any query, please feel free to ask.
Thanks a lot.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.