+ 2

how to write a program which shows output of sum of integers given by user?

for example user input 123456 output 21

1st Dec 2017, 3:36 PM
sahil
sahil - avatar
16 ответов
+ 2
#include <iostream> using namespace std; int main() { int x, total = 0, y; cin>>x; if (x != 0){ y = x%10; total += y; x /= 10; } if (x != 0){ y = x%10; total += y; x /= 10; } if (x != 0){ y = x%10; total += y; x /= 10; } if (x != 0){ y = x%10; total += y; x /= 10; } if (x != 0){ y = x%10; total += y; x /= 10; } cout<<total; return 0; } this is the first one with if statments but will only take 5 numbers total so 123456 won't work but 12345 will. to make more numbers work you need to copy and paste the if statement more. you can have more then needed so if you paste the if statement 20 times it will take up to 20 numbers but still will except 12345. but having some kind of loop is better even if it's just a while loop.
1st Dec 2017, 7:57 PM
Mooaholic
Mooaholic - avatar
+ 2
#include <iostream> using namespace std; int main() { int x, total = 0, y; cin>>x; for(; x != 0; x /= 10){ y = x%10; total += y; } cout<<total; return 0; } this will do what you are asking. or if you want to simplify it more: #include <iostream> using namespace std; int main() { int x, total = 0, y; cin>>x; for(; x != 0; y = x%10, total += y, x /= 10); cout<<total; return 0; }
1st Dec 2017, 4:09 PM
Mooaholic
Mooaholic - avatar
+ 2
when I enter 12345 into the program I get 15.
1st Dec 2017, 5:05 PM
Mooaholic
Mooaholic - avatar
+ 2
#include <iostream> using namespace std; int main() { int a, b, c, d; cin>>a>>b>>c; if ((a < b)&&(a > c)) d = a; if ((a > b)&&(a < c)) d = a; if ((b < a)&&(b > c)) d = b; if ((b > a)&&(b < c)) d = b; if ((c < b)&&(c > a)) d = c; if ((c > b)&&(c < a)) d = c; cout<<d; return 0; } will do the middle number with if statements. you want to do the first one with just if statements?
1st Dec 2017, 7:37 PM
Mooaholic
Mooaholic - avatar
+ 1
Does user enter 123456 in one time? or enters every number like firstly entered 1, then entered 2 ... ?
1st Dec 2017, 3:46 PM
Mustafa K.
Mustafa K. - avatar
+ 1
all one time
1st Dec 2017, 3:47 PM
sahil
sahil - avatar
+ 1
im not sure how to do the first one in just if statements, or you may need alot of them depending how many numbers you input.
1st Dec 2017, 7:48 PM
Mooaholic
Mooaholic - avatar
0
thnku
1st Dec 2017, 4:11 PM
sahil
sahil - avatar
0
this coding not actually showing output that i need
1st Dec 2017, 4:40 PM
sahil
sahil - avatar
0
i want if user input any number like 12345 and output is their sun which is 15
1st Dec 2017, 4:41 PM
sahil
sahil - avatar
0
ok its done.. working
1st Dec 2017, 6:03 PM
sahil
sahil - avatar
0
#include <iostream> using namespace std; int main() { int x, total = 0, y; cin>>x; for(; x != 0; x /= 10){ y = x%10; total += y; } cout<<total; return 0; } this will do what you are asking. or if you want to simplify it more: #include <iostream> using namespace std; int main() { int x, total = 0, y; cin>>x; for(; x != 0; y = x%10, total += y, x /= 10); cout<<total; return 0; } how it will done with if statemenr
1st Dec 2017, 6:55 PM
sahil
sahil - avatar
0
yes the first one also. plz
1st Dec 2017, 7:40 PM
sahil
sahil - avatar
0
its ok
1st Dec 2017, 7:49 PM
sahil
sahil - avatar
0
ok thnk u
1st Dec 2017, 7:58 PM
sahil
sahil - avatar
- 1
2nd problem is write a prrgram which takes three integers as a input and tell the 2nd maximum no.. for example input is 20 17 10 output is 17 bcz 17 is second maximum number
1st Dec 2017, 6:04 PM
sahil
sahil - avatar