The following incomplete program first asks the user to enter the number of items he/she has eaten today and then to enter the number of calories for each item. It then calculates the number of calories he/she has eaten for the day and displays the value. #include using namespace std; int main() { int numberOfItems; int count; //loop counter for the loop int caloriesForItem; int totalCalories; cout << "How many items did you eat today? "; cin >> numberOfItems; cout << "Enter the number of calories in each of the " << numberOfItems << " items eaten: " << endl; // ------------- Your code -------------- cout << "Total calories eaten today = " << totalCalories; return 0; }.complete the code.complete use a for loop to read in the calories of all the items. Test your program by entering 7 for the number of items and the following values for the calories: 7 120 60 150 600 1200 300 200
#include<iostream>
using namespace std;
int main()
{
int numberOfItems;
int count; //loop counter for the loop
int caloriesForItem;
int totalCalories;
cout << "How many items did you eat today? ";
cin >> numberOfItems;
cout << "Enter the number of calories in each of the " << numberOfItems << " items eaten: " << endl; // ------------- Your code --------------
int a[numberOfItems],i,f=0;
for(i=0;i<numberOfItems;i++)
{
cin>>a[i];
f=f+a[i];
}
totalCalories=f;
cout << "Total calories eaten today = " << totalCalories;
return 0;
}
Comments
Leave a comment