Home » Arrays(Dont Mess With Me) » ReverseNumberExample

ReverseNumberExample


import java.util.Scanner;

/**
 * Simple Java program to reverse a number in Java using loop and operator
 * This program also shows example of using division operator(/) and Remainder Operator(%)
 */
public class ReverseNumberExample {

public static void main(String args[]) {
 //input number to reverse
 System.out.println("Please enter number to be reversed using Java program: ");
 int number = new Scanner(System.in).nextInt();


 int z = number%10;
 System.out.println(z); // testing purpose

int reverse = reverse(number);
 System.out.println("Reverse of number: " + number + " is " + reverse(number));

 }

 /*
 * reverse a number in Java using iteration
 * @return reverse of number
 */
 public static int reverse(int number){
 int reverse = 0;
 int remainder ;
 do{
 remainder = number%10;
 reverse = reverse*10 + remainder;
 number = number/10;

 }while(number > 0);

 return reverse;
 }

}

 

 


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

%d bloggers like this: