Answer to Question #27639 in C++ for shibdayalsingh

Question #27639
input a number of digit display the sum of all digit
1
Expert's answer
2013-04-05T09:10:26-0400
#include<stdio.h>
#include<cstdlib>

usingnamespace std;

intmain() {
int n;
scanf ("%d", &n);
int res = 0;
if (n < 0) n = -n;
while (n) {
res += n % 10;
n /= 10;
}
printf ("%d
", res);
return 0;
}

OR

#include<stdio.h>
#include<cstdlib>
#include<iostream>
#include<string>

usingnamespace std;

intmain() {
string s;
getline(cin, s);
int res = 0;
for (int i = 0; i < s.length(); i++)
if (s[i] >= '0' && s[i] <= '9')
res += s[i] - '0';
cout << res << endl;
return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS