# Answer to Question #22526 in C++ for Otis

Question #22526

(Summing the digits in an integer) Write a function that computes the sum of the digits in an integer. Use the following function header.

int sumDigits (long n)

Write a test program that prompts the user to enter an integer and display the sum of all its digits.

int sumDigits (long n)

Write a test program that prompts the user to enter an integer and display the sum of all its digits.

Expert's answer

#include <conio.h>

#include <iostream>

using namespace std;

int sumDigits(long n)

{

char digit;

int result = 0;

while (n != 0)

{

digit = n % 10;

result += digit;

n /= 10;

}

return result;

}

void main()

{

long n;

cout << "Enter an integer number: ";

cin >> n;

cout << "The sum of its digits is: " << sumDigits(n) << endl;

getch();

}

#include <iostream>

using namespace std;

int sumDigits(long n)

{

char digit;

int result = 0;

while (n != 0)

{

digit = n % 10;

result += digit;

n /= 10;

}

return result;

}

void main()

{

long n;

cout << "Enter an integer number: ";

cin >> n;

cout << "The sum of its digits is: " << sumDigits(n) << endl;

getch();

}

Need a fast expert's response?

Submit orderand get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

## Comments

Assignment Expert23.01.13, 23:00You're welcome. We are glad to be helpful. If you really liked our service please press like-button beside answer field. Thank you!

otis23.01.13, 22:58Thank you!!.. really appreciate it!!

## Leave a comment