Create a DigitsSum application that prompts the user for a non-negative integer and then displays the sum of the digits. ( Java Programming )
import java.util.Scanner; public class DigitsSum { public static int sumOfDigits(int n) { int total = 0; while (n > 0) { total += (n % 10); n /= 10; } return total; } public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter a positive integer: "); int n = in.nextInt(); System.out.println("Sum of digits of " + n + " is " + sumOfDigits(n)); } }
Get Answers For Free
Most questions answered within 1 hours.