Powered By Blogger

Friday, December 24, 2010

C Program to find the sum of all digits in a number

/* C Program to find the sum of all digits in a given long number accepted from the user */

#include <stdio.h>
#include <stdlib.h>

int main()
{
    long number;
    int sum=0;
    printf("Enter a long number\n");
    scanf("%ld",&number);

    while(number>0)
    {
        sum=sum+(number%10);
        number=number/10;
    }
    printf("The sum of the digits is %d",sum);
    return 0;
}


No comments:

Post a Comment