C program to find sum of digits of a number

Category: C Program

Learn how to write a C program to find the sum of the digits of a number. This article provides a detailed explanation and sample code for this basic yet essential task in C programming.

Finding the sum of digits of a number is a fundamental problem in programming that helps in understanding basic arithmetic operations and loops. This article will guide you through writing a C program to find the sum of the digits of a given number, providing a detailed explanation and sample code.

Steps to Find the Sum of Digits

To solve this problem, follow these steps:

  1. Input the Number: Read the number from the user.
  2. Extract Each Digit: Use a loop to extract each digit of the number.
  3. Sum the Digits: Add each extracted digit to a sum variable.
  4. Print the Result: Output the sum to the console.

Write a C program to find sum of digits of a number

Here's a C program to find the sum of the digits of a number:

#include <stdio.h>

int main() {
    int number, digit, sum = 0;

    // Input the number
    printf("Enter an integer: ");
    scanf("%d", &number);

    // Calculate the sum of digits
    while (number != 0) {
        digit = number % 10;  // Extract the last digit
        sum += digit;         // Add the digit to the sum
        number /= 10;         // Remove the last digit
    }

    // Print the result
    printf("Sum of the digits: %d", sum);

    return 0;
}

Output

Enter an integer: 1234
Sum of the digits: 10

Explanation

  1. Input the Number: The program prompts the user to enter an integer. The scanf function reads the input number and stores it in the variable number.
  2. Extract Each Digit:
    • The while loop continues as long as number is not equal to 0.
    • The last digit is extracted using the modulus operator (%). For example, if number is 1234, digit = 1234 % 10 will result in digit = 4.
  3. Sum the Digits:
    • The extracted digit is added to the variable sum. For the initial iteration, sum = 0 + 4 results in sum = 4.
    • The last digit is removed from number using integer division (/). For example, number = 1234 / 10 results in number = 123.
  4. Repeat: The loop continues, extracting and summing each digit until number is 0.
  5. Print the Result: The program prints the sum of the digits to the console using the printf function.

Detailed Steps

  • Step 1: Input the Number
    • Use printf to prompt the user for input.
    • Use scanf to read the number from the user.
  • Step 2: Extract Each Digit
    • Use a while loop to continue processing as long as number is not zero.
    • Use the modulus operator (%) to get the last digit of the number.
  • Step 3: Sum the Digits
    • Add the extracted digit to the sum variable.
    • Remove the last digit from the number using integer division (/).
  • Step 4: Print the Result
    • Use printf to display the sum of the digits.

Example

For an input of 1234, the program will:

  • Extract 4 and add it to sum (sum = 4).
  • Extract 3 and add it to sum (sum = 7).
  • Extract 2 and add it to sum (sum = 9).
  • Extract 1 and add it to sum (sum = 10).

The output will be Sum of the digits: 10.


Recommended Posts