[Solved] What is the logic is used in this program [closed]


We beginners should help each other.:)

Here you are.

#include <stdio.h>

unsigned int digits_sum( unsigned int value )
{
    const unsigned int Base = 10;
    unsigned int sum = 0;

    do { sum += value % Base; } while ( value /= Base );

    return sum;
}

int main( void ) 
{
    unsigned int value = 12345;

    printf( "The sum of digits of number %u is %u\n", value, digits_sum( value ) ); 

    return 0;
}

The program output is

The sum of digits of number 12345 is 15

The logic is simple. To get the last digit of a number you should apply operator %. Then you need to divide the number by 10 that in the next step to get the digit before the last and so on.

1

solved What is the logic is used in this program [closed]