+ 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; }
10 Antworten
+ 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.
+ 2
Instead of using a variable and constantly over writing it, I'd suggest using an array or vector.
+ 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
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;
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.
0
Yes you are right but you need to subtract from total which will be already available as I think but what you want?
0
I want it to give me the normal subtraction.. Which is 5
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.
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?