reverse the digits in Java


Reverse digits in Java:

This program prints reverse of a number i.e. if the input is 1234 then output will be 4321.

 

 

 

 //Write a program in Java to reverse the digits

import java.util.Scanner;
class ReverseNumber{
public static void main(String args[])

{
int n, reverse = 0;
System.out.println(“Enter the number to reverse”);
Scanner in = new Scanner(System.in);
n = in.nextInt();
while( n != 0 )

{
reverse = reverse * 10;
reverse = reverse + n%10;
n = n/10;

}
System.out.println(“Reverse of entered number is “+reverse);

}

}

output:

rev

Related Posts

Comments are closed.