Sum of digits of a Number

Definition & Explanation

The goal is to find the sum of the digits of an input integer in a programming language. To achieve this, we break down the number into its individual digits and add them one by one by extracting them using the modulo operator (%).

Example:
Input: 12345
Output: 1 + 2 + 3 + 4 + 5 = 15

Explaination

  1. Objective: Break down an input integer into individual digits and calculate their sum.
  2. Modulo Operator (%): Extract digits by using this operator.
  3. Divide Operator (/): Reduce the number by one digit at a time with this operator.
C

Method 1 :

#include <stdio.h>

int main() {
    // Given input number
    int n = 12345;

    // Initialize variables to store the sum and remainder
    int sum = 0;
    int rem;

    // Loop until the entire number is processed
    while (n > 0) {
        // Extract the last digit using the modulo operator
        rem = n % 10;

        // Add the extracted digit to the sum
        sum = sum + rem;

        // Reduce the number by one digit using division
        n = n / 10;
    }

    // Output the final sum of digits
    printf("Sum of digits: %d\n", sum);

    return 0;
}

Output :

Sum of digits: 15
C++

Method 1 :

#include <iostream>

int main() {
    // Given input number
    int n = 12345;

    // Initialize variables to store the sum and remainder
    int sum = 0;
    int rem;

    // Loop until the entire number is processed
    while (n > 0) {
        // Extract the last digit using the modulo operator
        rem = n % 10;

        // Add the extracted digit to the sum
        sum = sum + rem;

        // Reduce the number by one digit using division
        n = n / 10;
    }

    // Output the final sum of digits
    std::cout << "Sum of digits: " << sum << std::endl;

    return 0;
}

Output :

Sum of digits: 15
JAVA

Method 1 :

public class Main {
    public static void main(String[] args) {
        // Given input number
        int n = 12345;

        // Initialize variables to store the sum and remainder
        int sum = 0;
        int rem;

        // Loop until the entire number is processed
        while (n > 0) {
            // Extract the last digit using the modulo operator
            rem = n % 10;

            // Add the extracted digit to the sum
            sum = sum + rem;

            // Reduce the number by one digit using division
            n = n / 10;
        }

        // Output the final sum of digits
        System.out.println("Sum of digits: " + sum);
    }
}

Output :

Sum of digits: 15
Python

Method 1 :

# Given input number
n = 12345

# Initialize variables to store the sum and remainder
sum = 0

# Loop until the entire number is processed
while n > 0:
    # Extract the last digit using the modulo operator
    rem = n % 10

    # Add the extracted digit to the sum
    sum = sum + rem

    # Reduce the number by one digit using division
    n = n // 10

# Output the final sum of digits
print("Sum of digits:", sum)

Output :

Sum of digits: 15