ADD ALL DIGITS OF A NUMBER (SUM) (JAVA)
import
java.util.Scanner;
public
class SumOfDigits {
public static void main(String
args[]) {
int number,
digit, sum = 0;
Scanner sc = new
Scanner(System.in);
System.out.print("Enter
the number: ");
number =
sc.nextInt();
while(number
> 0) {
digit
= number % 10;
sum
= sum + digit;
number
= number / 10;
}
System.out.println("Sum
of Digits: "+sum);
}
}
Comments
Post a Comment
If you have any doubts, Feel free to Ask !