Write a function that receives an integer number and returns

Write a function that receives an integer number and returns the sum of the digits as an integer. For example, if the input is 123, the sum of digits would be 6 because 1 + 2 + 3. Do not make any assumption about the number of digits in your input. You are not allowed to use characters, strings or character of arrays.

Solution

#include <stdio.h>

int main()
{
   int n;
   printf(\"Please enter an integer:\");
   scanf(\"%d\",&n);
   int sum=0;
   int tmp=n;
   while(n>0)
   {
       sum = sum+n%10;
       n = n/10;
   }
   printf(\"The sum of all the digits in %d is %d\ \",tmp,sum);
   return 0;
}

 Write a function that receives an integer number and returns the sum of the digits as an integer. For example, if the input is 123, the sum of digits would be

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site