Given a number, write a program using while loop to reverse the digits of the number. For example, the number 12345 should be written as 54321. Also find the sum of all digits, even digits and odd digits.

Below is a Java program that takes a number as input, reverses its digits, and calculates the sum of all digits, the sum of even digits, and the sum of odd digits using a while loop.

				
					import java.util.Scanner;

public class ReverseNumberAndSumDigits {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // Requesting input from the user
        System.out.print("Enter a number: ");
        int num = scanner.nextInt();

        int originalNum = num;
        int reversedNum = 0;
        int sumAll = 0, sumEven = 0, sumOdd = 0;

        while (num != 0) {
            int remainder = num % 10;
            reversedNum = reversedNum * 10 + remainder;
            sumAll += remainder;

            if (remainder % 2 == 0) {
                sumEven += remainder;
            } else {
                sumOdd += remainder;
            }

            num /= 10;
        }

        // Output results
        System.out.println("Reversed number: " + reversedNum);
        System.out.println("Sum of all digits: " + sumAll);
        System.out.println("Sum of even digits: " + sumEven);
        System.out.println("Sum of odd digits: " + sumOdd);
    }
}

				
			

Explanation:

  1. Variable Initialization:

    • num: stores the original number input by the user.
    • reversedNum: stores the reversed number.
    • sumAll, sumEven, sumOdd: store the sums of all digits, even digits, and odd digits respectively.
  2. Input Request:

    • The program requests a number from the user and stores it in num.
    • The original number is saved in originalNum for reference.
  3. While Loop:

    • The loop continues until num becomes 0.
    • In each iteration:
      • The last digit of num is obtained using num % 10 and stored in remainder.
      • This digit is then added to reversedNum after shifting reversedNum to the left by one digit.
      • The digit is also added to sumAll.
      • Depending on whether the digit is even or odd, it is added to sumEven or sumOdd.
      • num is then divided by 10 to remove the last digit.
  4. Output Results:

    • After the loop, the reversed number, the sum of all digits, the sum of even digits, and the sum of odd digits are printed.

Output:

Here are example outputs of the program for different inputs:

  1. Example 1:

				
					Enter a number: 12345
Reversed number: 54321
Sum of all digits: 15
Sum of even digits: 6
Sum of odd digits: 9

				
			

Example 2:

				
					Enter a number: 24680
Reversed number: 8642
Sum of all digits: 20
Sum of even digits: 20
Sum of odd digits: 0


				
			

Example 3:

				
					Enter a number: 13579
Reversed number: 97531
Sum of all digits: 25
Sum of even digits: 0
Sum of odd digits: 25



				
			

To compile and run this program, you can use a Java compiler:

  1. Save the code in a file named ReverseNumberAndSumDigits.java.
  2. Open a terminal and navigate to the directory containing the file.
  3. Compile the program using the javac compiler:
				
					javac ReverseNumberAndSumDigits.java



				
			

Run the compiled program using the java interpreter:

				
					java ReverseNumberAndSumDigits




				
			

You should see the output displayed as shown above.

Scroll to Top