Java has been a versatile, general-purpose, robust, class-based, and object-oriented programming language that has as few dependencies as possible. Before we talk about reversing a particular number in java, let us first understand what reversing a number in any language means and why it is relevant and its uses and applications. Reversing a number implies that the value of the unit of a particular number will become the nth digit value if the number is n digits long and tens digit number will become (n-1)th place value number on and so forth. In this article will see a detailed explanation of reverse number in java.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
To understand the concept further, we are required to take the help of an example. Let us say the number is 120, then the number is 3 digits long and contains the place value of units, tens and hundreds. Now when we apply the logic of reversing a number in java, then the units digit, i.e. 0, will become our hundreds digit, and 1 will become our units digit, and 2 will remain as is. Therefore the new number obtained will be 021, and this will be the reverse number of 120. The logic or the program to write is more or less similar in every programming language, and therefore reading this logic in java should not be a problem.
Now, let us discuss the relevance of a reverse number and why do we actually need it. Sometimes there becomes a need to take out the palindrome of a number. If you are new to programming and are unfamiliar with the concept of palindrome, then palindrome is the number that is the same even upon reversal. For example, number 5885 is a palindrome of 5885. So it becomes mandatory to fetch all those values or records which contain palindrome numbers, and there becomes a need to make use of reversing a number in java.
Let us understand below with the help of various examples for the reverse numbers in java.
But first, let us understand the algorithm behind the reversing of numbers.
Following are the different examples:
In this case, to find out the reverse of a number in java, we are using a while loop, a pre-conditioned loop. The condition here is related to the use of > operator where the check is related to the number if it has a value greater than 0.
Code:
public class Main { public static void main(String[] args) { int num=569; int r_num=0; while(num>0) { r_num=(r_num*10)+(num%10); num=num/10; } System.out.println(r_num); } }
Output:
In this program, the compiler will scan the code at least once and iterates once the condition matches. This is the basic difference between the do-while loop and the while loop. This is a post-condition loop, whereas the while loop is a preconditioned loop.
Code:
public class Main { public static void main(String[] args) { int num=569; int r_num=0; do { r_num=(r_num*10)+(num%10); num=num/10; } while(num>0); System.out.println(r_num); } }
Output:
Code:
class main{ public static void main(String[] args) throws IOException { int num, c = 0, n1; num = 569 n1 = num; while(n1 > 0) { c++; n1 = n1 / 10; } Rev_Rec obj = new Rev_Rec(); int x = obj.reverse(num, c); System.out.println("Reversal of a number is:"+x); } int reverse(int a, int len) { if(len == 1) { return a; } else { int b = a % 10; a = a / 10; return (int) ((b * pow(10, len - 1)) + reverse(a, --len)); } } }
Output:
In this case, to find out the reverse of a number in java, we are using a while loop, a pre-conditioned loop. The condition here is related to the use of != operator where the check is related to the number if it has the value not equal to 0.
Code:
public class Main { public static void main(String[] args) { int num=569; int r_num=0; while(num!=0) { r_num=(r_num*10)+(num%10); num=num/10; } System.out.println(r_num); } }
Output:
Here in this section, we are going to read about the alternative way of finding the reverse of a number in Java to the one explained above. This will be done with the help of for loop, which is another kind of a looping construct just like the while loop which we studied above. The for loop is generally used at places where we know the number of iterations to be done.
Code:
public class Main { public static void main(String[] args) { int n = 123, rev = 0; for(;n != 0; n /= 10) { int letter = n % 10; rev = rev * 10 + letter; } System.out.println(rev); } }
Output:
Ici, dans cet article de blog, nous avons découvert le nombre inversé en Java et comment trouver l'inversion d'un nombre. Nous avons également étudié les différentes applications et utilisations de la recherche du nombre inverse. Cela peut être utilisé à de nombreuses fins en fonction des besoins. Il peut y avoir d'autres façons d'écrire le même code, expliquées dans cet article. Il s’agit de l’approche la plus optimale et la plus largement utilisée pour trouver l’inverse d’un nombre. L'approche pour inverser un nombre est presque la même dans tous les langages de programmation ; par conséquent, une fois que vous connaîtrez la logique du programme, il vous sera très facile d'implémenter cette solution également dans d'autres langues. J'espère que vous avez aimé notre article. Restez à l'écoute de notre blog pour plus d'articles.
Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!