Write a program in Java to reverse the digits of a number using while loop

 

AIM: Write a program in Java to reverse the digits of a number using while loop

CODE:

import java.util.Scanner;

public class reverse

{

            public static void main(String args[])

            {

                         Scanner readme = new Scanner(System.in);

                         System.out.println("Enter Number " );

                        int n1,n2,ans=0;

                        n1 = readme.nextInt();

                        while(n1!=0)

                        {

                                    n2=n1%10;

                                    ans=ans*10+n2;

                                    n1=n1/10;

                                   

                        }

                         System.out.println("reverse="+ans);

            }

}

           

OUTPUT:



Comments

Popular posts from this blog

Write an application that illustrates method overriding in the same package and different packages. Also demonstrate accessibility rules in inside and outside packages

Describe abstract class called Shape which has three subclasses say Triangle, Rectangle, and Circle. Define one method area()in the abstract class and override this area() in these three subclasses to calculate for specific object.

Write a Java Program with class Rectangle having instance variable width, length, area and colour. Create methods like setData() and area().Create two object of Rectangle and compare their area and colour. If area and colour both are the same for the objects then display “Matching Rectangles”, otherwise display “Non matching Rectangle”.