Professional Documents
Culture Documents
L10 Loops SumDigits CPP
L10 Loops SumDigits CPP
// Chapter 4 : CFE
//
#include <iostream>
int sumDigits()
{
int x = 1234;
int n = x;
int sum = 0;
int rem = 0;
while(n)
{
rem = n % 10;
sum += rem;
n = n / 10;
}
cout << "Sum of digits of " << x << ": " << sum << endl;
return sum;
}
int main()
{
int count=0;
while(count < 5)
{
sumDigits();
++count;
}
return 0;
}
Question:
Have a look at the sample run.
What is the reason of the wrong answer when 11111111111 is entered?