Program to add digits of a number in C

#include <stdio.h>

int main()
{
   int n, sum = 0, remainder;

   printf("Enter an integern");
   scanf("%d",&n);

   while(n != 0)
   {
      remainder = n % 10;
      sum = sum + remainder;
      n = n / 10;
   }

   printf("Sum of digits of entered number = %dn",sum);

   return 0;
}

Output

Enter an integer

1234

sum of digits of enterted number =10

Leave a Reply

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

Live Updates COVID-19 CASES