SCHOOL OF CODE BUILDERS
Learn To CODE. Become A DEVELOPER.
Pages
HOME
DATA STRUCTURES
STRINGS
ARRAYS
MATRIX
BINARY TREES
LINKED LIST
STACK
QUEUE
SORTING
SEARCHING
C
PYTHON
PSEUDOCODE
CONTEST PROBLEMS
ALGORITHMS
PATTERNS
PHP
C PUZZLES
C INTERVIEW QUESTIONS
JAVA
C++
HASHING
RECURSION
BASIC C PROGRAMS
TCS-CODEVITA
FACEBOOK
CONTACT US
Java program to add digits of a number
Java program to add digits of a number
Output:
Enter a number to get sum of its digits: 1234
The sum of digits of number entered is: 10
Implementation:
import java.util.*; import java.lang.*; import java.io.*; class CodeRegister { public static void main (String[] args) throws java.lang.Exception { Scanner in = new Scanner(System.in); System.out.println("Enter a number to get sum of its digits:"); int number = in.nextInt(); int sum = 0; while(number!=0) { sum += (number%10); number /= 10; } System.out.println("The sum of digits of number entered is: "+sum); } }
PREVIOUS
NEXT
HOME