About 8,200,000 results
Open links in new tab
  1. How to sum digits of an integer in java? - Stack Overflow

    Nov 24, 2014 · In Java 8, public int sum(int number) { return (number + "").chars() .map(digit -> digit % 48) .sum(); } Converts the number to a string and then each character is mapped to it's digit value by subtracting ascii value of '0' (48) and added to the final sum.

  2. Java Program for Sum the digits of a given number

    Feb 10, 2023 · Given a list of numbers, write a Java program to find the sum of all the elements in the List using for loop. For performing the given task, complete List traversal is necessary which makes the Time Complexity of the complete program to O(n), where n is the length of the List. Example: Input : List

  3. java - How to sum 3 digits of an integer? - Stack Overflow

    Mar 20, 2020 · How to sum digits of an integer in java? It is not necessary convert the number to string, use the % operator to get the separate digits of an integer. something like this: int sum = 0; while (num > 0) { sum += num % 10; num = num / 10; …

  4. Sum of Digits of a Number - GeeksforGeeks

    Feb 7, 2025 · Given a number n, find the sum of its digits. Examples : The idea is to add the digits starting from the rightmost (least significant) digit and moving towards the leftmost (most significant) digit.

  5. Sum of Digits of a Number in Java - Tpoint Tech

    In order to find the sum of digits of a number, we must familiar with the Java loops and operators. Enter any integer number as input. After that, we use modulus and division operation respectively to find the sum of digits of the number as output. Let's see the steps. Declare a variable (sum) to store the sum of numbers and initialize it to 0.

  6. Sum of the digits of a three-digit number - java help on CodeGym

    Write the code for sumDigitsInNumber (int number). The method takes a three-digit whole number. You need to calculate the sum of the digits of this number, and then return the result. The sumDigitsInNumber method is called with the argument 546. The program must not read data from the keyboard.

  7. java - How to find sum of first three digits - Stack Overflow

    You can do the extraction of digit values without the string creation using Character.getNumericValue(char). –

  8. Java Program to Find Sum of Digits in a Number - Tutorial Gateway

    In this article we will show you, how to Write a Program to Find Sum of Digits in Java using For Loop, While Loop, Functions and Recursion.

  9. Java Program Sum Of digits Of A Number | Programs - Java

    Apr 17, 2025 · Here we will discuss the various methods to calculate the sum of the digits of any given number with the help of Java Programs. The compiler has been added so that you can execute the program yourself, alongside suitable examples and sample outputs.

  10. Java Program to find Sum of Digits | CodeToFun

    Oct 30, 2024 · Compile and run the program to find the sum of digits for the specified number. The program defines a class SumOfDigits containing a static method sumOfDigits that takes an integer number as input and returns the sum of its digits. Inside the method, it uses a while loop to iterate through each digit of the number.

  11. Some results have been removed
Refresh