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(System.in);
      //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);
   }
}

Output:

Input your number and press enter: 
56789111
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.

Join Our Newsletter.

Subscribe to CrowdforThink newsletter to get daily update directly deliver into your inbox.

CrowdforJobs is an advanced hiring platform based on artificial intelligence, enabling recruiters to hire top talent effortlessly.

CrowdforJobs

CrowdforApps brings to you the well researched list of the most successful and finest App development companies, Web software developers.

CrowdforApps

CrowdforGeeks is where lifelong learners come to learn the skills they need, to land the jobs they want, to build the lives they deserve.

CrowdforGeeks

CrowdforThink is a leading Indian media and information platform, known for its end-to-end coverage of the Indian startup ecosystem.

CrowdforThink
CFT

News & Blogs

f5bc2fea6240f198c0119d6fe4a92e7c.png

How to reverse a number using while loop in Java

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

dcc7d9d20769f5894731297e6e4aa8bd.png

How to reverse a number using recursion in Java

import java.util.Scanner; class RecursionReverseDemo { //A method for reverse public stati...

4caeed8b52ccf6bfe56a91ea3c5cc8b8.png

Write a Java Program to calculate simple interest.

Simple Interest Formula Simple Interest = (P × R × T)/100 P is Principal amount. R ...

Top Authors

With good communication and writing skiils, Astha Sharma is a full-time content writer working wi...

Astha Sharma

Overall 3+ years of experience as a Full Stack Developer with a demonstrated history of working i...

Lokesh Gupta

Amit Agrawal Founder and COO at Cyber Infrastructure (P) Limited which is an custom software deve...

Amit Agrawal

SB Components Ltd is a specialist UK manufacturer of protective cases for single board compu...

SB Components
CFT

Our Client Says