Sum of a Digit of a Number

Definition & Explanation

The “sum of digits” of a number refers to the total obtained by adding up each individual digit in the number. This process involves breaking down the number into its constituent digits and then performing addition on those digits.

For example, consider the number 123. To find the sum of its digits:

  • Digit 1: The first digit is 1.
  • Digit 2: The second digit is 2.
  • Digit 3: The third digit is 3.

The sum of digits is calculated as 1+2+3=61+2+3=6.

Program Logic

  1. Input:
    • First, the program prompts the user to enter a number.
  2. Initialization:
    • We declare variables to store the number (number), the current digit being processed (digit), and the sum of digits (sum). Initially, sum is set to 0.
  3. Extracting Digits:
    • We use a while loop to extract digits from the number one at a time.
    • In each iteration of the loop:
      • We calculate the last digit of the number by taking the remainder when divided by 10 (number % 10). This gives us the rightmost digit of the number.
      • We add this digit to the sum (sum += digit).
      • We then remove the last digit from the number by integer division by 10 (number /= 10). This effectively shifts the digits to the right, removing the last digit.
    • This process continues until the number becomes 0, indicating that we have processed all the digits.
  4. Output:
    • Finally, the program prints the sum of the digits (sum).
  5. Termination:
    • The program ends.

The key concept here is to repeatedly extract the last digit of the number using the modulo operator %, add it to the sum, and then remove it from the number by integer division. This process is repeated until the number becomes 0, effectively processing all its digits.

C

Method 1 :

#include <stdio.h>

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

    printf("Enter a number: ");
    scanf("%d", &number);

    while (number > 0) {
        digit = number % 10;
        sum += digit;
        number /= 10;
    }

    printf("Sum of digits: %d\n", sum);

    return 0;
}

Output :

6
C++

Method 1 :

#include <iostream>

using namespace std;

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

    cout << "Enter a number: ";
    cin >> number;

    while (number > 0) {
        digit = number % 10;
        sum += digit;
        number /= 10;
    }

    cout << "Sum of digits: " << sum << endl;

    return 0;
}

Output :

6
JAVA

Method 1 :

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int number, digit, sum = 0;

        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a number: ");
        number = scanner.nextInt();

        while (number > 0) {
            digit = number % 10;
            sum += digit;
            number /= 10;
        }

        System.out.println("Sum of digits: " + sum);
    }
}

Output :

Sum of digit 6
Python

Method 1 :

def get_sum(n):
	sum=0
	for digit in str(n):
		sum+=int(digit)
	return sum

n=123
print(get_sum(n))

Output :

6