How to Reverse a Number in Java


The example is to reverse a given number in Java.

Source Code

package com.beginner.examples;

public class ReverseNumberExample {

    public static void main(String[] args){
    	int num = 2019;
    	int m = num;
		int reNum  = 0;
		int n = 0;
				
		//reverse the number
		while(m > 0){
			// modulus
			n = m % 10;
			m = m / 10;
			reNum = reNum * 10 + n;
		}
		System.out.println("Reversed Number is " + reNum + ".");
	}
}

Output:

Reversed Number is 9102.
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments