Write a program to reverse a number [Java Interview Question & Answer]

Write a program to reverse a number

This article will help you to understand “Java Program to reverse number”

We need to write a program to reverse a number using numeric operations. We can also reverse it using string operations as used in Palindrome Example

Java Program to reverse number

package com.codenuclear;


public class ReverseGivenNumber {
public int reverseNumber(int inputNo){
		int reverseNum = 0;
		while(inputNo != 0){
			reverseNum = (reverseNum*10)+(inputNo%10);
			inputNo = inputNo/10;
		return reverseNum;
	public static void main(String a[]){
		ReverseGivenNumber obj = new ReverseGivenNumber();
		System.out.println("Reversed Number : "+obj.reverseNumber(123456789));
		System.out.println("Reversed Number : "+obj.reverseNumber(12000));


Output Reversed Number : 987654321 Reversed Number : 21

Note:- Limitations of this program is, if the input has trailing zero like 12000 as an example, then reversed number will not consider this zero in leading position and as per numeric nature, leading zero will not be there in output.

To overcome this limitation, we can use String logic as Palindrome Example