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.
This version is using loop for to solve the problem. */

#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 */
    /* loop for, centinel-controlled repetition */
    for ( ; number > 0; number /= 10){

      sum += number % 10;
    }

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

    return 0;
}