Answer to Question #27639 in C++ for shibdayalsingh
2013-04-02T06:53:17-04:00
input a number of digit display the sum of all digit
1
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 !
Learn more about our help with Assignments:
C++
Comments
Leave a comment