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 a program in Java to demonstrate use of final class.

Write a java program to create 3 threads using Thread class. Three threads should calculate the sum of 1 to 5, 6 to 10 and 11to 15 respectively. After all thread finishes main thread should print the sum and average.

Write a program in Java to demonstrate single inheritance, multilevel inheritance and hierarchical inheritance.