how to sum of digits in java code example
Example 1: Java program to find the sum of all the digits in the inputted number
long number, sum;
Scanner sc = new Scanner(System.in);
System.out.println("Enter any DIGIT number: ");
number = sc.nextLong();
sc.close();
for (sum = 0; number != 0; number /= 10) {
sum += number % 10;
}
System.out.println("ForLoop Sum of ALL digits: " + sum);
Example 2: sum of digits in java
import java.io.*;
public class sd
{
public static void main(String [] args)throws IOException
{
InputStreamReader hi = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(hi);
System.out.println("Enter the number");
int num=Integer.parseInt(in.readLine());
int sum=0;
while(num>=0)
{
int rem=n;
sum+=rem;
int quo=n/10;
n=quo;
}
System.out.println(sum);
}
}