0

Write a program that prompts the user to input an integer and then outputs bothe the individual digits of the number and the sum of digits,for example the input is :123456,the outputs are 1 2 3 4 5 6 and sum=21

30th Nov 2016, 1:33 PM
teena
3 Respuestas
+ 1
#include<iostream.h> main() { int i,sum=0; for(i=1;i<=6;i++) { cout<<i<<" "; sum+=i;} cout<<endl<<"sum="<<sum;}
30th Nov 2016, 9:00 PM
Faroug AS Hashad
Faroug AS Hashad - avatar
0
Thank you both
1st Dec 2016, 12:51 PM
teena
- 1
#include <iostream> using namespace std; int main() { int num = 123456; int s = 0; int t = 1000000000; int sum = 0; while(t){ s = num / t; if(s){ s %= 10; cout << s << endl; sum += s; } t /= 10; } cout << "sum: " << sum << endl ; return 0; } NOTE: This will work only for signed integer If the nos. of digits are known change the value of "t" accordingly it may save some loop iteration and if check can be avoided. plz VOTE UP if this helped :)
1st Dec 2016, 6:42 AM
Abhishek Kumar
Abhishek Kumar - avatar