Sum Of Digits Of A Number Using Recursion | Java Programs

Output:

*** Sum Of Digits Of A Number Using Recursion ***

Enter the number: 197
Sum Of Digits : 17

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);
    }
}
Share This :

Related Post



sentiment_satisfied Emoticon