Skip to content
  • SiteMap
  • Our Services
  • Frequently Asked Questions (FAQ)
  • Support
  • About Us

UpdateGadh

Update Your Skills.

  • Home
  • Projects
    •  Blockchain projects
    • Python Project
    • Data Science
    •  Ai projects
    • Machine Learning
    • PHP Project
    • React Projects
    • Java Project
    • SpringBoot
    • JSP Projects
    • Java Script Projects
    • Code Snippet
    • Free Projects
  • Tutorials
    • Ai
    • Machine Learning
    • Advance Python
    • Advance SQL
    • DBMS Tutorial
    • Data Analyst
    • Deep Learning Tutorial
    • Data Science
    • Nodejs Tutorial
  • Blog
  • Contact us
  • Toggle search form
Finding the Sum of Digits

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

Posted on August 15, 2024August 15, 2024 By Updategadh No Comments on 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.

Table of Contents

  • Finding the Sum of Digits: A Multilanguage Approach
    • 1. Java
    • 2. Python
    • 3. C++
    • 4. JavaScript
    • Conclusion

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.
https://updategadh.com/how-to/use-social-media/

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.

https://updategadh.com/php-project/car-rental-system-in-php-and-mysql/
Post Views: 691
code Snippets Tags:Collage Project, Finding the Sum of Digits, java and mysql, Python

Post navigation

Previous Post: How to Building Career in Data Science: Student’s Guide
Next Post: DES Encryption Program Code Using Java JFrame

More Related Articles

Simple Complaint Management System in Python with Source Code - Simple Complaint Management System in Python Simple Complaint Management System in Python with Source Code code Snippets
Top 10 Online Platforms for Practicing Coding Challenges Top 10 Online Platforms for Practicing Coding Challenges code Snippets
Blockchain Security - What is Blockchain Security code Snippets

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You may also like

  1. Supply Chain Management PHP and CSS
  2. Book Library in JavaScript With Free Code
  3. F1 Race Road Game in Python Free Source Code
  4. Student Management System in Python With Free Code
  5. Create Address Book in Python with Source Code
  6. Contact Management in Python with Source Code

Most Viewed Posts

  1. Top Large Language Models in 2025
  2. Online Shopping System using PHP, MySQL with Free Source Code
  3. login form in php and mysql , Step-by-Step with Free Source Code
  4. Flipkart Clone using PHP And MYSQL Free Source Code
  5. News Portal Project in PHP and MySql Free Source Code
  6. User Login & Registration System Using PHP and MySQL Free Code
  7. Top 10 Final Year Project Ideas in Python
  8. Blog Site In PHP And MYSQL With Source Code || Best Project
  9. Online Bike Rental Management System Using PHP and MySQL
  10. E learning Website in php with Free source code
  • AI
  • ASP.NET
  • Blockchain
  • ChatCPT
  • code Snippets
  • Collage Projects
  • Data Science Project
  • Data Science Tutorial
  • DBMS Tutorial
  • Deep Learning Tutorial
  • Final Year Projects
  • Free Projects
  • How to
  • html
  • Interview Question
  • Java Notes
  • Java Project
  • Java Script Notes
  • JAVASCRIPT
  • Javascript Project
  • JSP JAVA(J2EE)
  • Machine Learning Project
  • Machine Learning Tutorial
  • MySQL Tutorial
  • Node.js Tutorial
  • PHP Project
  • Portfolio
  • Python
  • Python Interview Question
  • Python Projects
  • PythonFreeProject
  • React Free Project
  • React Projects
  • Spring boot
  • SQL Tutorial
  • TOP 10
  • Uncategorized
  • Real-Time Medical Queue & Appointment System with Django
  • Online Examination System in PHP with Source Code
  • AI Chatbot for College and Hospital
  • Job Portal Web Application in PHP MySQL
  • Online Tutorial Portal Site in PHP MySQL — Full Project with Source Code

Most Viewed Posts

  • Top Large Language Models in 2025 (8,616)
  • Online Shopping System using PHP, MySQL with Free Source Code (5,226)
  • login form in php and mysql , Step-by-Step with Free Source Code (4,875)

Copyright © 2026 UpdateGadh.

Powered by PressBook Green WordPress theme