Programming Challenge Chapter 3 – Q #7 How Many Calories? – Tony Gaddis – Starting Out With C++

Programming Challenge Chapter 3 – Q #7 How Many Calories? – Tony Gaddis – Starting Out With C++

How many Calories

Problem: -

A bag of cookies holds 40 cookies. The calorie information on the bag claims that there are 10 servings in the bag and that a serving equals 300 calories. Write a program that asks the user to input how many cookies he or she actually ate and then reports how many total calories were consumed.

Solution: -

#include <iostream>

using namespace std;

int main()

{

      const float bag_of_cookies = 40.0,

            serving = 10.0,

            serving_per_bag = 300.0;

 

      float cookies_per_serving = bag_of_cookies / serving_per_bag;

      float cookies_eat, serving_eat, consumed_eat;

 

      cout << "How Many Cookies Eaten: ";

      cin >> cookies_eat;

 

      serving_eat = cookies_eat / cookies_per_serving;

      consumed_eat = serving_eat * serving_per_bag;

 

      cout << "Cookies Eaten    =    " << cookies_eat << endl;

      cout << "Calories Consumed =    " << consumed_eat << endl;

 

      return 0;

}

This is the solution of this question




OUTPUT OF THIS QUESTION 

Input is highlighted with yellow color.



Explanation of this Solution

  1. Declare three float constants and initialize given values.
  2. Declare a float variable for per serving calculation.
  3. Declare three float variables for calculation and input.
  4. Take input from the user by using cin.
  5. Calculate the values.
  6.  In last, display output on the screen.
  7. Return 0 to the main function.

Also, I attach CPP file of this problem. You can download this file

Click Here to Download This File




Thanks!  

Post a Comment

0 Comments