60 688
Assignments Done
97,9%
Successfully Done
In April 2018

Answer to Question #27639 in C++ for shibdayalsingh

Question #27639
input a number of digit display the sum of all digit
Expert's answer
#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 first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions