How to use sum of digits of any number in C Language

Sum of Digits

On this page

Sum of Digits

Program to use Sum of Digits of any Number in C Language

#include <stdio.h>
int main()
{
  int n, sum = 0, rem;
  printf("Enter a Number :");
  scanf("%d", &n);
  while (n > 0)
  {
      rem = n % 10;
      sum += rem;
      n /= 10;
  }
  printf("Sum of Digits=%d\n", sum);
  return 0;
}

Output

Enter a Number
1452
Sum of Digits=12