Question: #include / / FORWARD DECLARATIONS int digitSum ( int number ) ; int main ( int argc, char * argv [ ] ) { int

#include
// FORWARD DECLARATIONS
int digitSum(int number);
int main(int argc, char * argv[])
{
int num;
int sum;
// Prompt for, and receive user input
printf("Enter a number whose digits should be summed: ");
if (scanf("%d", &num)!=1)
{
printf("Sorry, you didn't enter a number.
");
return 1;
}
// Calculate the sum of the digits of the provided number
sum = digitSum(num);
// Print out the result.
printf("The sum of the digits of %d is %d
", num, sum);
// Return success
return 0;
}
/**
* Calculate and return the sum of the digits of a given number.
*
* @param number
* The number whose digits are to be summed
*
* @return
* The sum of the number's digits
*/
int digitSum(int number)
{
int digit;
int sumOfDigits =0;
while (number !=0)
{
// Get the next digit
digit = number /10;
// Add this digit to the sum
sumOfDigits = sumOfDigits + digit;
// Prepare for next digit
number = number /10;
}
// Give 'em the sum
return sumOfDigits;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!