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
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;}
0
Thank you both
- 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 :)