Java Programming | Reverse of number

Introduction

This is a tutorial for reverse of number in java. The program is given below that takes number from user and prints its reverse. The is not extendable. Go enjoy the program. Lets begin…….

Program for reverse of number in java.

//import Scanner as we require it.
import java.util.Scanner;

// the name of our class its public
public class ReverseNo {
	//void main
		public static void main (String[] args)
		{
			//declare int
			int no,revno=0,m;

			//Declare input as scanner
			Scanner input = new Scanner(System.in);

			//Take input
			 System.out.println("Enter Number :");
			 no = input.nextInt();

			//add while loop
			while(no>0)
			{
				m=no%10;
				revno=(revno*10)+m;
				no=no/10;
			}

			//Print reverse no.
			System.out.println("Reverse of no is "+revno);
		}
}

Output

Enter Number :
1234
Reverse of no is 4321

How does it work

  1. You enter the number.
  2. The reverse is created using loop and is saved.
  3. The reverse is printed.

Extending it

The program cannot be extended.

Explanation.

  1. Import the Scanner.
  2. Declare the class as public
  3. Add the void main function
  4. Add system.out.println() function with the message to enter number.
  5. Declare input as Scanner.
  6. Take the inputs and save it in variables.
  7. Add a loop
  8. Add system.out.println() function to print the reverse of number.

At the end.

You learnt creating the Java program for Reverse of number. So now enjoy the program.

Please comment on the post and share it.
And like it if you liked.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s