Java Online Compiler
Example: Sum of Digits of a Number in Java using For loop
C
C++
C#
Java
Python
PHP
Main.java
STDIN
Run
// Sum of Digits of a Number in Java using For loop import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("Enter the integer number::\n"); int x = in.nextInt(), sum = 0, m; System.out.print ("The sum of " + x + " digits is = "); while (x > 0) { m = x%10; sum = sum+m; x = x/10; } System.out.println (sum + "\n"); } }
1459
Output
Clear
ADVERTISEMENTS