Answer to Question #225882 in C++ for Ulando

Question #225882

Write a function to compute the sum of the digits in an integer. For example sum Digits(234)returns 2+3+4=9


1
Expert's answer
2021-08-16T01:03:37-0400
#include <iostream>


using namespace std;


int Digit(int n) {
        int sum = 0;
        while (n != 0) {
            sum = sum + n % 10;
            n = n / 10;
        }
        return sum;
}


int main()
{
        int ans = Digit(463);
        cout << ans << '\n';

        return 0;
}   

Output: 
13

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