+ 2
how to write a program which shows output of sum of integers given by user?
for example user input 123456 output 21
16 Answers
+ 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.
+ 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;
}
+ 2
when I enter 12345 into the program I get 15.
+ 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?
+ 1
Does user enter 123456 in one time? or enters every number like firstly entered 1, then entered 2 ... ?
+ 1
all one time
+ 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.
0
thnku
0
this coding not actually showing output that i need
0
i want if user input any number like 12345
and output is their sun which is 15
0
ok its done.. working
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
0
yes the first one also. plz
0
its ok
0
ok thnk u
- 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