KnowledgeBoat Logo

Computer Applications

Write a program to input a number and display the new number after reversing the digits of the original number. The program also displays the absolute difference between the original number and the reversed number.

Sample Input: 194
Sample Output: 491
Absolute Difference= 297

Java

Java Iterative Stmts

ICSE

104 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;
        }
        
        int diff = revNum - orgNum;
        System.out.println("Reversed Number = " + revNum);
        System.out.println("Absolute Difference = " + Math.abs(diff));
    }
}

Output

BlueJ output of Write a program to input a number and display the new number after reversing the digits of the original number. The program also displays the absolute difference between the original number and the reversed number. Sample Input: 194 Sample Output: 491 Absolute Difference= 297

Answered By

37 Likes


Related Questions