C Language

May 23, 2023

How to use sum of digits of any number in c language

program to use sum of digits of any number in c language


#include <stdio.h>
int main()
{
    int n, sum = 0, rem;
    printf("enter a number : ");
    scanf("%d", &n);
    while (n > 0)
    {
        rem = n % 10;
        sum += rem;
        n /= 10;
    }
    printf("sum of digits = %d\n", sum);
    return 0;
}

output


enter a number : 55
sum of digits = 10

by : Ajaj Khan

Quick Summary:

In this program a number is being entered, the number being entered is being divided by ten, the remainder is being added to the same number, then the number entered is divided by ten. The quotient that is left after dividing again by ten, the remainder that is coming, add the same remainder againbeingĀ given