Write a function to compute the sum of the digits in an integer. For example sum Digits(234)returns 2+3+4=9
#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
Comments
Leave a comment