which formula should I use to count this? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

which formula should I use to count this?

"Mr. John is a baker. He wants to count how many breads he have made today. There is a number of breads written in every pan. Because he is already tired, he needs your help." input: A series of number named Bx which shows number in every x-pan output: Y which is the total number of breads example: input: 5 6 7 8 output: 26 -- how do I count this?

23rd Sep 2016, 9:19 AM
seirz
seirz - avatar
2 Answers
+ 3
Not sure if you use cin or have an array ready with the inputs, but here are examples of what you can do: int count = 0; int B = 42; //main loop, enter a negative number to exit it while (B > 0) { cin >> B; if (B > 0) { count += B; } } cout << count << endl; int count = 0; int B[4] = {5, 6, 7, 8}; for (int i = 0; i < 4; i++) { count += B[i]; } cout << count << endl;
23rd Sep 2016, 11:07 AM
Zen
Zen - avatar
+ 2
wow I didn't think of using an array before! Thank you very much, it works perfectly fine! :)
23rd Sep 2016, 5:45 PM
seirz
seirz - avatar