C program to find the sum of digits of a number

 

#include<stdio.h>

int main()
{
    int num, rem, sum = 0;

    // Ask user to enter the number
    printf("Enter a number: ");

    // Read two number from the user
    scanf("%d", &num);

    while(num != 0)
    {
        rem = num % 10;
        sum += rem;
        num = num / 10;
    }

    printf("sum = %d", sum);

    return 0;
}

Output:

Enter a number: 27624
sum = 21

 

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *