Tuesday, October 4, 2011

Displaying the reverse of a number (Java)

//This Java program accepts a number from the
//user and the prints the reverse of the
//number.
//Sample Input : 591
//Sample Output : 195
import java.util.*;
class reverseOfNumber
{
public static void main()
{
    System.out.println("Enter a number");
    Scanner sc=new Scanner(System.in);
    int num_orig=sc.nextInt();
    int num_new=0,n=num_orig;
    while(n>0)
    {
        num_new=(num_new*10)+(n%10);
        n=n/10;
    }
    System.out.println("The reversed number is "+num_new);
}
}
//Author : Mayank Rajoria

No comments:

Post a Comment