tag:blogger.com,1999:blog-8712770457197348465.post86137105838178229..comments2024-03-18T23:01:42.768-07:00Comments on Javarevisited: Java Program to Reverse an Integer Number - Example tutorialjavin paulhttp://www.blogger.com/profile/15028902221295732276noreply@blogger.comBlogger26125tag:blogger.com,1999:blog-8712770457197348465.post-17135370298909339382023-08-04T03:16:37.571-07:002023-08-04T03:16:37.571-07:00public class NumberReverseRecursion {
public sta...public class NumberReverseRecursion {<br /><br /> public static String reverse(int num, String rev) {<br /><br /> if (num == 0)<br /> return rev;<br /> rev = rev + num % 10;<br /> return reverse(num / 10, rev);<br /> }<br /><br /> public static void main(String[] args) {<br /> System.out.println("Enter the number : ");<br /> Scanner sc = new Scanner(System.in);<br /> String rev Krishnanoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-54180354898601958972022-09-23T03:04:28.667-07:002022-09-23T03:04:28.667-07:00static int result =0;
static int reverse(int n...static int result =0;<br /> static int reverse(int n){<br /> if(n==0){<br /> return 0;<br /> }<br /> int rem = n%10;<br /> result = result*10+rem;<br /> reverse(n/10);<br /> return result;<br /> }Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-58296855553458620112022-08-21T20:05:30.680-07:002022-08-21T20:05:30.680-07:00Hello Anonymous, do you mean recursive method? I h...Hello Anonymous, do you mean recursive method? I have never heard of recurring method but if you mean how to solve this problem using recursion, just replace the code in the loop into recursive call. If you find it difficult, I can add that into code as well. javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-66662834472944368752022-08-20T11:31:31.159-07:002022-08-20T11:31:31.159-07:00I need recurring method of this reverse I need recurring method of this reverse Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-81160266962320453562020-04-17T23:50:30.314-07:002020-04-17T23:50:30.314-07:00you can check positive and negative number like gr...you can check positive and negative number like greater than zeor or less than zero, or you can use bitwise operator, check this article, <a href="http://javarevisited.blogspot.sg/2013/01/how-to-check-if-number-is-positive-or-negative-java-example.html#axzz5E2uHdG3w" rel="nofollow">how to check if number is positive or negative in Java</a> for some code examples. javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-76792249413300411382020-04-16T03:32:05.218-07:002020-04-16T03:32:05.218-07:00I want to display as number should be positive whe...I want to display as number should be positive when it's less than zero while reverse the number???what's the codeAnonymoushttps://www.blogger.com/profile/08494745623454909606noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-12704935292234173812020-02-04T05:17:23.861-08:002020-02-04T05:17:23.861-08:00Hello mrunal, isn't following the same method ...Hello mrunal, isn't following the same method will work? just replace + with - to add into a negative number. javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-68484102020462483062020-02-03T19:59:44.748-08:002020-02-03T19:59:44.748-08:00how to reverse negative numbers? example -100 to -...how to reverse negative numbers? example -100 to -001 or -5461 to -1645?mrunalhttps://www.blogger.com/profile/13659856133681740648noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-26412023846567939212018-09-26T19:03:47.377-07:002018-09-26T19:03:47.377-07:00Hello One23drumm, that's valid solution but wi...Hello One23drumm, that's valid solution but will not be accepted in most of the interview because they want you to solve without using Java framework methods like reverse(). Try solving by using your own logic and without reverse method. javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-54821076498452741882018-09-26T10:12:50.658-07:002018-09-26T10:12:50.658-07:00How about this?
Integer.parseInt(new StringBuilder...How about this?<br />Integer.parseInt(new StringBuilder(String.valueOf(number)).reverse().toString())One23drummhttps://www.blogger.com/profile/09545711050056691924noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-2883436095760930232015-12-22T22:03:54.970-08:002015-12-22T22:03:54.970-08:00Write an object oriented java program to display t...Write an object oriented java program to display the arithmetic operation reverse of a number using switch case<br /><br />Plzzzzzz give me this program fastAnonymoushttps://www.blogger.com/profile/06836111468768397681noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-82558925718590336002015-09-19T10:14:25.859-07:002015-09-19T10:14:25.859-07:00How can I reverse a number without using any loop?...How can I reverse a number without using any loop?Jitendra Mohantyhttps://www.blogger.com/profile/14783032723857243302noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-57784696316754247762015-02-20T11:48:13.786-08:002015-02-20T11:48:13.786-08:00how to solve if integer starting with zero??? (ex....how to solve if integer starting with zero??? (ex. 0321, 042)Naveen KTRnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-2753809625165437302015-01-06T05:49:05.175-08:002015-01-06T05:49:05.175-08:00how to reverse a number like 10,100,500,1000.........how to reverse a number like 10,100,500,1000.............etcAnonymoushttps://www.blogger.com/profile/04317893962292124361noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-38379039123187437262013-10-21T07:36:09.326-07:002013-10-21T07:36:09.326-07:00Nice post friend!
but there's a ERROR in the a...Nice post friend!<br />but there's a ERROR in the above code, which make it UNABLE to REVERSE a huge list of NUMBERS.....<br /><br />*ERROR: when i entered 120 its output came out -> 21 only. so its not giving right output for the numbers ending with ZEROS(0) like 100,1500 etc.<br /><br />I corrected the CODE and HERE it is : <br />http://www.codenirvana.in/2013/10/Udithttps://www.blogger.com/profile/04765807266420924989noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-53235279343973792292013-08-05T03:59:25.163-07:002013-08-05T03:59:25.163-07:00how we reverse 3 digit no. & not more than 3 d...how we reverse 3 digit no. & not more than 3 digit no. is reverse and not less than 2 digit no. in javaAnonymoushttps://www.blogger.com/profile/10997947337940780099noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-15608388102855078582013-08-02T06:49:28.615-07:002013-08-02T06:49:28.615-07:00@Anonymous, This should work on any number of digi...@Anonymous, This should work on any number of digit? are you facing any issue?Javin @ abstract class vs interface javahttp://javarevisited.blogspot.com/2013/05/difference-between-abstract-class-vs-interface-java-when-prefer-over-design-oops.htmlnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-33680648658049607132013-08-02T05:22:06.010-07:002013-08-02T05:22:06.010-07:00how to apply this logic for 3 digit numberhow to apply this logic for 3 digit numberAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-37832965097745574842013-04-29T12:46:40.595-07:002013-04-29T12:46:40.595-07:00package reversedigit;
import java.util.Scanner;
...package reversedigit;<br /><br />import java.util.Scanner;<br /><br /> class ReverseDigit {<br /><br /> <br /> public static void main(String[] args)<br /> {<br /> int c,a=0,b=0;<br /> System.out.println("Enter Digit to Reverse It");<br /> Scanner sc = new Scanner(System.in);<br /> c=sc.nextInt();<br /> <br /> System.out.println("Reverse Order of &Mokshnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-61552164437055207492013-01-20T15:25:55.352-08:002013-01-20T15:25:55.352-08:00is there a way to do this without the use of loops...is there a way to do this without the use of loops, if statements and strings?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-15464012712399908912012-06-02T08:31:48.833-07:002012-06-02T08:31:48.833-07:00Hi Rob,
I understand the question as 'literal...Hi Rob, <br />I understand the question as 'literal' reversal of number in which case we would be missing the zero's at the end. If we just consider the actual integer value after reversal, what you said is right.Madhanhttps://www.blogger.com/profile/02714030836897606835noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-64222810476376905262012-05-15T11:30:35.648-07:002012-05-15T11:30:35.648-07:00Your blog is very good!
But, for this program, we...Your blog is very good!<br /><br />But, for this program, we would get wrong output if the input integer ends with "0". So instead of saving the remainder in to a int, instead we could use a string buffer and append it. Finally showing the stringbuffer as final ouput would be one option.Madhanhttps://www.blogger.com/profile/02714030836897606835noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-88704719478320615282012-04-26T06:10:56.631-07:002012-04-26T06:10:56.631-07:00Yes Jan, It's converting String input to int b...Yes Jan, It's converting String input to int but that's the easiest way to get input from User, Though you can also use JOptionPane to display input dialog to input numbers but I stick to simpler one.Javin @ String to double in Javahttp://javarevisited.blogspot.com/2011/10/convert-double-to-string-example.htmlnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-27984735587428249512012-04-26T00:01:45.062-07:002012-04-26T00:01:45.062-07:00Yeah, it's going to be tough to write somethin...Yeah, it's going to be tough to write something _without_ touching the API at all.<br />In your example, just reading in the String to be converted to an int and then reversed requires the Scanner to read and the nextInt() to translate the next token to an int.Anonymoushttps://www.blogger.com/profile/14335719314813410020noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-82774074388926720462012-04-25T06:18:14.799-07:002012-04-25T06:18:14.799-07:00@Jan, you are right on your part, you can reverse ...@Jan, you are right on your part, you can reverse number by treating them as String in Java but this programming exercise is meant to do it without any API method and with just arithmetic operator in order to apply some kind of logic but no doubt it can be done by converting String to integer and using your solution.Javin @ String to int conversion javahttp://javarevisited.blogspot.com/2011/08/convert-string-to-integer-to-string.htmlnoreply@blogger.com