Write a java program to find reverse of a number using Strings?

This is one of the important java interview programming question asked for freshers and 0-3 years of experience.Here are the steps given below to find reverse of a number :

Steps for finding reverse of a number :

 Ex :Input: 123

Output : 321

 1)  Take an input number.

2)  Convert number to String.

3)  Take another string reverse and initialize it to empty.

4)  Extract each character from input string from rightside and add it to the reverse variable.

5)  Repeat this process for all characters in the string.

Java example to reverse a number using strings

public class ReverseNumberDemo {

	public static void main(String[] args) {

		Integer number=123;
		String numberString=String.valueOf(number);
		String reverse="";
		for(int i=numberString.length()-1;i>=0;i--){
			reverse=reverse+numberString.charAt(i);
		}

		System.out.println(""+Integer.valueOf(reverse));
	}

}

Leave a Reply

Your email address will not be published. Required fields are marked *