Tuesday , December 6 2016
Home / Blue J / Java program for reversing a number

Java program for reversing a number

This program reverses a number given as a input. This java program for reversing a number uses a concept of dividing the given number by 10 and adding the remainder to the units digit in  new number everytime. You can add to a unit place everytime by multiplying a number by 10 first(which will leave unit digit as 0) and then a number.

rev=(rev*10) + num%10;
let:  num=123; rev=0 (initially)

Iteration 1: num%10 = 3;
Therefore: rev= rev*10   + num%10  = 0*10 + 3 = 3 ;

Iteration 2: num=num/10 ; thus num=12;  Now, num%10 = 2;
Therefore:  rev= rev*10  +  num%10  = 3*10 + 2 = 32 ;

Iteration 3: num/=10; thus num=1; Now, num%10= 1;
Therefore:  rev= rev*10  +  num%10  = 32*10 + 1 = 321 ;


Reverse number
Reversing a number

Incoming terms: program for revering a number.

Also See:   Program to count the number of alphabets, digits, spaces, special symbols

Check Also

Program for Matrix Multiplication in JAVA

Matrix Multiplication:– Here is given a Java Program for Matrix Multiplication also call matrix product. In ...

Leave a Reply

Your email address will not be published. Required fields are marked *

Preventing spam * Time limit is exhausted. Please reload the CAPTCHA.