How to reverse a number using for loop in Java

How to reverse a number using for loop in Java

How to reverse a number using for loop in Java

The program will prompt user to input the number and then it will reverse the same number using loop loop.

import java.util.Scanner;
class ForLoopReverseDemo
   public static void main(String args[])
      int num=0;
      int reversenum =0;
      System.out.println("Input your number and press enter: ");
      //This statement will capture the user input
      Scanner in = new Scanner(;
      //Captured input would be stored in number num
      num = in.nextInt();
      /* for loop: No initialization part as num is already
       * initialized and no increment/decrement part as logic
       * num = num/10 already decrements the value of num
      for( ;num != 0; )
          reversenum = reversenum * 10;
          reversenum = reversenum + num%10;
          num = num/10;

      System.out.println("Reverse of specified number is: "+reversenum);


Input your number and press enter: 
Reverse of specified number is: 11198765


Author Biography.

Lokesh Gupta
Lokesh Gupta

Overall 3+ years of experience as a Full Stack Developer with a demonstrated history of working in the information technology and services industry. I enjoy solving complex problems within budget and deadlines putting my skills on PHP, MySQL, Python, Codeigniter, Yii2, Laravel, AngularJS, ReactJS, NodeJS to best use. Through Knowledge of UML & visual modeling, application architecture design & business process modeling. Successfully delivered various projects, based on different technologies across the globe.

News & Blogs


What It Was Like to Write a Full Blown Flutter App

1. Porting an iOS App to Flutter Since my last post about Flutter months ago, I felt that the lo...


You Don’t Need Loops in JavaScript

1. To Loop Through All Elements and Get an new modified array Using loop: var names = ["Jack",...


How to Create a Chatbot Using Dialogflow

Chatbots are artificially intelligent programs by which we can make our websites more interactive...