Introduction to Computer Science

/*
Source File: LoopSum.c
Author: Yana
This program sums the digits of a non-negative integer.
Input:a a non-negative integer
Output: The sum of the digits of the given input number
Algorithm: Use division by 10 and modulo by 10 in order to
extract each time the least significant digit.
*/

#include<stdio.h>

int main(){

    int number = 0; /* the input number */
    int sum = 0; /* the sum of digits of the input */

    /* input session */
    printf("Please enter a non-negative integer number: ");
    scanf("%d", &number);

    /* find sum of digits of input number */
    /* centinel-controlled repetition */
    while( number > 0){

      /* extract current LSG (least significant digit) and add it to sum. */
      sum += number % 10;
      number /= 10;
    }

    /* output result */
    printf( "The sum of digits is: %d\n", sum);

    return 0;
}