Tuesday, December 24, 2013

Write a C program to add digits of a number

Description-Here we are using modulus operator (%) to extract individual digits of number and adding them.

Source Code:
#include <stdio.h>
int 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;

}

No comments:

Post a Comment