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


Discover more from PlusDigit

Subscribe to get the latest posts sent to your email.

Discover more from PlusDigit

Subscribe now to keep reading and get access to the full archive.

Continue reading

AdBlock Detected!

Please disable your AdBlocker to access this website.

Once disabled, this message will disappear automatically.