How do I convert the given program to subtraction. So instead of total= no. +no.+no. I want it to be total = no. - no. -no.? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How do I convert the given program to subtraction. So instead of total= no. +no.+no. I want it to be total = no. - no. -no.?

#include <iostream> using namespace std; int main() { int num = 1; int number; int total = 0; while (num <= 5) { cin >> number; total += number; num++; } cout << total << endl; return 0; }

19th Oct 2019, 9:47 PM
Ismail Olanrewaju
Ismail Olanrewaju - avatar
9 Answers
+ 1
Ismail Olanrewaju Yes now you got it. That's why here total = total - no. in loop. So each time total will change and you will get your final result. It should be like this :- total = 18 - 9; total = 9 - 1; total = 8 - 1; total = 7 - 1; total = 6 - 1 = 5 which is your final result.
20th Oct 2019, 6:31 AM
A͢J
A͢J - avatar
+ 2
Instead of using a variable and constantly over writing it, I'd suggest using an array or vector.
19th Oct 2019, 11:45 PM
Stephen Matthew
Stephen Matthew - avatar
+ 1
You are performing addition of given 5 values bcz here num is 5, for every iteration you scanned a number value that is added to total, the output depends on what the value given to number
20th Oct 2019, 7:19 AM
🐆 Janaki Ramudu 🅓🅞🅝 🇮🇳
🐆 Janaki Ramudu 🅓🅞🅝   🇮🇳 - avatar
0
In this code total += number means total = total + number not total = number + number So in case of subtraction you can do like this. total -= number; Or total = total - number;
19th Oct 2019, 10:01 PM
A͢J
A͢J - avatar
0
But if I input 5 digits it won't properly subtract the 5 numbers.. Let's say I input 9 1 1 1 1 A Normal subtraction will give us 5 as the answer right? But in this case it's finna be "0 -9 -1 -1 -1 -1 " Which will give us a negative 13 (-13). So that isn't the answer that I want.
19th Oct 2019, 11:00 PM
Ismail Olanrewaju
Ismail Olanrewaju - avatar
0
Yes you are right but you need to subtract from total which will be already available as I think but what you want?
19th Oct 2019, 11:12 PM
A͢J
A͢J - avatar
0
I want it to give me the normal subtraction.. Which is 5
19th Oct 2019, 11:32 PM
Ismail Olanrewaju
Ismail Olanrewaju - avatar
0
How 5? Can you explain me. You are taking input from keyboard. When you will decide from which value you will have to subtract? You need already given value from which you will have to subtract inputs value. I think your concept is not clear.
19th Oct 2019, 11:37 PM
A͢J
A͢J - avatar
0
Oh I understand now.. so I have to change my "total " To a fixed amount so therefore when I subtract, it will give me 5.. . I understand now. I have to make my total like 18 so when I minus 9,1,1,1,1 it will be something like "18 - 9 - 1- 1-1-1 which will give us a 5... Right?
19th Oct 2019, 11:55 PM
Ismail Olanrewaju
Ismail Olanrewaju - avatar