CSCI 151 Introduction to Computer Science

/*

Source File: sumDigit.c
Author: Yana
Date: YEAR/MONTH/DAY
This program sums the digits of a 3-digit number
Input: a 3-digit non-negative integer
Output: The sum of the digits of the given input number
Algorithm: Extract the digits using division by 10 and remainder operation ( modulo 10).
*/

#include<stdio.h >

int main(){

    int number = 0;   /* input number, declaration and initialization */
    int sum = 0;       /* sum of the digits, declaration and initialization */

    printf("Please enter a 3-digit number: "); /* user prompt */
    scanf ("%d", &number);

    /* extract last digit (units) and add the extracted digit to the sum */
    sum = sum + number % 10;
    /* leave the number with two other digits */
    number = number / 10;

    /* extract middle digit (tens) and add the extracted digit to the sum */
    sum = sum + number % 10;
    /* leave the number with one digit (hundreds)*/
    number = number / 10;

    /* extract the remaining digit (hundreds) and add the extracted digit to the sum */
    sum = sum + number % 10;

    /* print the results */
    printf("The sum of digits is: %d\n", sum);

    return 0;
}