Martes, Oktubre 16, 2012

Write a program to add digits of a number using modulus operator.

Output:
Enter a number
12358
Sum of digits entered number  = 19

Code:
#include <stdio.h>
 
main()
{
   int n, sum = 0, remainder;
 
   printf("Enter an integer\n");
   scanf("%d",&n);
 
   while(n != 0)
   {
      remainder = n % 10;
      sum = sum + remainder;
      n = n / 10;
   }
 
   printf("Sum of digits of entered number = %d\n",sum);
 
   return 0;
}

Walang komento:

Mag-post ng isang Komento