Computer Applications

Write a program in Java to read a number and display its digits in the reverse order. For example, if the input number is 2468, then the output should be 8642.

Output:
Enter a number: 2468
Original number: 2468
Reverse number: 8642

Java

Java Iterative Stmts

2 Likes

Answer

import java.util.Scanner;

public class KboatDigitReverse
{
    public static void main(String args[]) {
        
        Scanner in = new Scanner(System.in);
        System.out.print("Enter Number: ");
        int orgNum = in.nextInt();
        
        int copyNum = orgNum;
        int revNum = 0;
        
        while(copyNum != 0) {
            int digit = copyNum % 10;
            copyNum /= 10;
            revNum = revNum * 10 + digit;
        }
        
        System.out.println("Original Number = " + orgNum);
        System.out.println("Reverse Number = " + revNum);
    }
}

Output

Answered By

3 Likes


Related Questions