In: Computer Science
A bag of cookies contains 40 cookies. The calorie information printed on the bag claims that there are 8 servings in each bag and that a single serving equals 300 calories. Design and write a C++ program that prompts your user for the number of cookies that he or she ate and then displays the calories they consumed.
Use variables to represent the given information, such as the number of cookies in each bag and the number of calories in each serving. Compose statements using the given variables to calculate the number of calories in each cookie. Use the number of cookies and the number of calories per cookie to determine how many calories were consumed. The use of magic is not permitted in computer programming. You, as the programmer must compose steps that store given information in variables and then use these variables to determine values such as the number of calories in each cookie.
CODE IN C++
Answer-
Your code is given below As the requirement-
#include<iostream>
using namespace std;
int Cookies_Function() {
int Num_cookies;
cout << "Enter how many cookies you consume?" <<
'\n';
cin >> Num_cookies; //take input
return Num_cookies;
}
double total_calories(int Num_cookies) {
double cookies_per_serving = 40 / 8; // 8 servings in each
bag
double calories_per_serving = 300; //single serving equals 300
calories
double total_consumed_cookies = Num_cookies / cookies_per_serving *
calories_per_serving;
return total_consumed_cookies;
}
int main() {
int Num_cookies = Cookies_Function();
double total_consumed_cookies = total_calories(Num_cookies);
cout << "======== you consumed :-" <<" "<<
total_consumed_cookies << " calories===========." <<
'\n'; //display the output
return 0;
}
Screenshot of output:
Note- Please do upvote, if any problem then comment in
box sure I will help.