Sunday, 15 December 2013

C Program to Compute the Sum of Digits in a given Integer

#include < stdio.h>
 
void main()
{
    long num, temp, digit, sum = 0;
 
    printf("Enter the number \n");
    scanf("%ld", &num);
    temp = num;
    while (num > 0)
    {
        digit = num % 10;
        sum  = sum + digit;
        num /= 10;
    }
    printf("Given number = %ld\n", temp);
    printf("Sum of the digits %ld = %ld\n", temp, sum);
}

Output

 Enter the number
 300 
Given number =  300 
Sum of the digits  300  =  3 
 
   
Enter the number
 16789 
Given number =  16789 
Sum of the digits  16789  =  31 

For More Details Please Visit Ictjobs.info

No comments: