Output:
Click Here For Java Online Compiler
Solution:
*** Sum Of Digits Of A Number *** Enter the number: 345 The number is: 345 Sum of digits : 12
Click Here For Java Online Compiler
Solution:
import java.util.Scanner; public class SumDigitsOfNumber { public static void main(String args[]) { Scanner scanner = new Scanner(System.in); System.out.println("*** Sum Of Digits Of A Number ***" + "\n"); System.out.print("Enter the number: "); int number = scanner.nextInt(); int sum = 0; int num = number; System.out.println("The number is: " + number); while (num > 0) { int lastDigit = num % 10; sum += lastDigit; num /= 10; } System.out.println("Sum of digits : " + sum); } }
comment 0 comments:
more_vertsentiment_satisfied Emoticon