Sum of digits for a given 5-digit number – C Program


Write a C program to find and display the sum of all digits for a given 5-digit number. [June 2015, Set-4]

#include <stdio.h>

int main()
{
    int number, temp, remainder, sum = 0;

    number = 12587;

    temp = number;

    while (temp > 0)
    {
        remainder = temp % 10;
        sum += remainder;
        temp /= 10;
    }

    printf("sum of digits of number %d is %d", number, sum);

    return 0;
}