C Program Find Sum of Digits of N

By | March 13, 2020

C Program Find Sum of Digits of N

/* C program to find sum of digits
of a number

www.easycodebook.com

*/

#include <stdio.h>

int main()
{
   int n, num, sum = 0, digit;

   printf("Enter a number to find sum of its digits\n");
   scanf("%d", &n);

   num = n;

   while (num != 0)
   {
      digit = num % 10;
      sum = sum + digit;
      num = num / 10;
   }

   printf("Sum of digits of %d = %d\n", n, sum);

   return 0;
}

Output

Enter a number to find sum of its digits
786
Sum of digits of 786 = 21

Leave a Reply

Your email address will not be published. Required fields are marked *