Finding the Sum of Digits

Finding the Sum of Digits: Multi language Java, Python, Js

Finding the Sum of Digits: A Multilanguage Approach

When working with numbers in programming, one common task is to find the sum of their digits. This is a fundamental problem that helps in understanding basic arithmetic operations and programming logic. In this post, we’ll explore how to compute the sum of digits using four different programming languages: Java, Python, C++, and JavaScript. Each implementation will illustrate the approach in its respective language, showcasing syntax and logic variations.

1. Java

In Java, we can calculate the sum of digits using a simple while loop. Here’s how you can do it:

public class FindSumOfDigits {
    public static int sumOfDigits(int number) {
        int sum = 0;
        while (number != 0) {
            sum += number % 10;
            number /= 10;
        }
        return sum;
    }

    public static void main(String[] args) {
        int number = 12345;
        System.out.println("Sum of digits: " + sumOfDigits(number));
    }
}

Explanation:

  • We use the modulo operator % to get the last digit of the number.
  • The last digit is added to sum, and then the number is divided by 10 to remove the last digit.
  • This process repeats until the number is zero.

2. Python

Python offers a more concise syntax to achieve the same result. Here’s a Python implementation:

def sum_of_digits(number):
    total = 0
    while number > 0:
        total += number % 10
        number //= 10
    return total

number = 12345
print("Sum of digits:", sum_of_digits(number))

Explanation:

  • The modulo operator % is used to extract the last digit.
  • The // operator performs integer division to remove the last digit from the number.
  • This continues until the number becomes zero.
See also  Book Library in JavaScript With Free Code

3. C++

In C++, the approach is similar to Java. Here’s how you can implement it:

#include <iostream>
using namespace std;

int sumOfDigits(int number) {
    int sum = 0;
    while (number != 0) {
        sum += number % 10;
        number /= 10;
    }
    return sum;
}

int main() {
    int number = 12345;
    cout << "Sum of digits: " << sumOfDigits(number) << endl;
    return 0;
}

Explanation:

  • We use a while loop to process each digit.
  • The last digit is added to sum, and then the number is divided by 10 to remove the processed digit.
  • The loop continues until the number is zero.

E-Commerce Website with Product Recommendation

4. JavaScript

JavaScript provides a straightforward way to handle this task as well. Here’s the code:

function sumOfDigits(number) {
    let sum = 0;
    while (number > 0) {
        sum += number % 10;
        number = Math.floor(number / 10);
    }
    return sum;
}

const number = 12345;
console.log("Sum of digits:", sumOfDigits(number));

Explanation:

  • The modulo operator % extracts the last digit of the number.
  • Math.floor is used to perform integer division, which removes the last digit from the number.
  • This continues until the number is reduced to zero.

Car Rental System in PHP and MYSQL

Conclusion

The problem of finding the sum of digits is a great exercise for learning fundamental programming concepts. Despite the differences in syntax and specific functions between languages, the underlying logic remains the same. Understanding how to implement this task in multiple languages can enhance your problem-solving skills and provide insights into language-specific features and idioms.

See also  Create a Snake Game in Java

Post Comment