In: Computer Science
Q1
Create a program that asks the user to input their budget. Then, it will ask the user to input each of their purchase until their entire budget is used up.
What kind of loop do you think is the most appropriate? You don't have to create the entire program. Part of it has already been created for you (see code below).
Note: Assume that the user always uses up their budget to the last cent and they don't over spend (is that really possible? :D )
You can use the following screen output to guide your program.
Please enter your budget: $2500.25 Please enter your purchase amount: $1200 Remaining balance: $1300.25 Please enter your purchase amount: $300.25 Remaining balance: $1000.00 Please enter your purchase amount: $1000.00 Remaining balance: $0.00
Skeleton code:
#include <iostream> #include <iomanip> int main() { double budget = 0, purchase = 0; std::cout << std::fixed << std::setprecision(2); std::cout << "Please enter your budget: $"; std::cin >> budget; // place code here return 0; }
Q2
Create a program that computes the summation of a number. For example, the summation of 3 would be 1 + 2 + 3.
What kind of loop do you think is the most appropriate? You don't have to create the entire program. Part of it has already been created for you (see code below).
You can use the following screen output to guide your program.
summation(n) program Please enter n: 3 The summation(3) is 6
Skeleton code:
#include <iostream> #include <iomanip> int main() { int summation = 0, n = 0; std::cout << "summation(n) program"; std::cout << "Please enter n: "; std::cin >> n;
// provide code here
std::cout << "summation("<< n << ") is: " << summation << "\n"; return 0; }
Q3
Question text
Create a program that asks the user to add all the values that a user inputs until they input -1.
What kind of loop do you think is the most appropriate? You don't have to create the entire program. Part of it has already been created for you (see code below).
Note: Be careful not to add the extra -1.
You can use the following screen output to guide your program.
Welcome to Summer-upper! Please enter a number you want to add (type -1 to exit): 10 Please enter a number you want to add (type -1 to exit): 3 Please enter a number you want to add (type -1 to exit): 11 Please enter a number you want to add (type -1 to exit): -1 Sum is: 24
Skeleton code:
#include <iostream> int main() { int sum = 0, input = 0; std::cout << "Welcome to Summer-upper!\n"; // provide code here std::cout << "Sum is: " << sum << "\n"; return 0; }
q1. I think do-while is more appropriate for solving the given problem statement. The reason is, do-while runs once even if the condition is false, which in this case is required.
Following is the code.
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double budget = 0, purchase = 0;
cout << fixed << setprecision(2);
cout << "Please enter your budget: $";
cin >> budget;
// place code here
do {
cout << "Please enter your purchase amount: $";
cin >> purchase;
double balance = budget - purchase;
budget = balance;
cout << "Remaining balance:
$"<<balance<<"\n";
}while(budget > 0);
return 0;
}
output q1.:
Q2: For this question, I just need to calculate the summation of
the numbers till n. For loop is ideal for such
scenarios.
code:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int summation = 0, n = 0;
cout << "summation(n) program";
cout << "Please enter n: ";
cin >> n;
// provide code here
for(int i=0;i<n;i++){
summation += (i+1);
}
cout << "summation("<< n << ") is: " <<
summation << "\n";
return 0;
}
output:
q3. For this case both do-while and while loop are ideal. But I
prefer, while loop to solve this problem.
#include <iostream>
using namespace std;
int main() {
int sum = 0, input = 0;
std::cout << "Welcome to Summer-upper!\n";
// provide code here
while(input >=0){
cout << "Please enter a number you want to add
(type -1 to exit):";
cin >> input;
if(input > -1)
sum += input;
}
cout << "Sum is: " << sum << "\n";
return 0;
}
output:
Let me know if you need any further help.