{"id":1398,"date":"2022-03-03T19:23:04","date_gmt":"2022-03-03T19:23:04","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1398"},"modified":"2022-03-03T19:23:04","modified_gmt":"2022-03-03T19:23:04","slug":"program-to-reverse-a-number-using-for-while-and-recursion","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/03\/03\/program-to-reverse-a-number-using-for-while-and-recursion\/","title":{"rendered":"Program to reverse a number using for, while and recursion"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\" id=\"using-while\">Program 1: Reverse a number using while Loop<\/h3>\n\n\n\n<p>In this program, we are taking the input number from the user using&nbsp;<code>Scanner<\/code>&nbsp;class and then we are reversing the number using while loop.<\/p>\n\n\n\n<p>The logic we are using here is: Inside the\u00a0while loop\u00a0we are dividing the given number by 10 using\u00a0% operator\u00a0and then storing the\u00a0remainder\u00a0in the\u00a0<code>reversenum<\/code>\u00a0variable after multiplying the\u00a0<code>reversenum<\/code>\u00a0by 10. we are repeating this step again and again until the given number become zero.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.Scanner;\nclass ReverseNumberWhile\n{\n   public static void main(String args&#91;])\n   {\n      int num=0;\n      int reversenum =0;\n      System.out.println(\"Input your number and press enter: \");\n      \/\/This statement will capture the user input\n      Scanner in = new Scanner(System.in);\n      \/\/Captured input would be stored in number num\n      num = in.nextInt();\n      \/\/While Loop: Logic to find out the reverse number\n      while( num != 0 )\n      {\n          reversenum = reversenum * 10;\n          reversenum = reversenum + num%10;\n          num = num\/10;\n      }\n\n      System.out.println(\"Reverse of input number is: \"+reversenum);\n   }\n}<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Input your number and press enter: \n145689\nReverse of input number is: 986541<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"using-for\">Program 2: Reverse a number using for Loop<\/h3>\n\n\n\n<p>This program is fairly similar to the first program, here we are using\u00a0for loop\u00a0instead of while loop.<\/p>\n\n\n\n<p>As you can see, in this program we have not used the initialization and increment\/decrement section of for loop because we have already initialized the variables outside the loop and we are decreasing the value of&nbsp;<code>num<\/code>&nbsp;inside for loop by diving it by 10.<\/p>\n\n\n\n<p>The logic is same as first program.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.Scanner;\nclass ForLoopReverseDemo\n{\n   public static void main(String args&#91;])\n   {\n      int num=0;\n      int reversenum =0;\n      System.out.println(\"Input your number and press enter: \");\n      \/\/This statement will capture the user input\n      Scanner in = new Scanner(System.in);\n      \/\/Captured input would be stored in number num\n      num = in.nextInt();\n      \/* for loop: No initialization part as num is already\n       * initialized and no increment\/decrement part as logic\n       * num = num\/10 already decrements the value of num\n       *\/\n      for( ;num != 0; )\n      {\n          reversenum = reversenum * 10;\n          reversenum = reversenum + num%10;\n          num = num\/10;\n      }\n\n      System.out.println(\"Reverse of specified number is: \"+reversenum);\n   }\n}<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Input your number and press enter: \n56789111\nReverse of specified number is: 11198765<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"using-recursion\">Program 3: Reverse a number using recursion<\/h3>\n\n\n\n<p>Here we are using&nbsp;<strong>recursion<\/strong>&nbsp;to reverse the number. We have defined a method&nbsp;<code>reverseMethod()<\/code>&nbsp;and we are passing the input number to this method.<\/p>\n\n\n\n<p>This method then divides the number by 10, displays the&nbsp;<strong>remainder<\/strong>&nbsp;and then calls itself by passing the&nbsp;<strong>quotient<\/strong>&nbsp;as parameter. This process goes on and on until the number is in single digit and then it displays the last digit (which is the first digit of the number) and ends the recursion.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.Scanner;\nclass RecursionReverseDemo\n{\n   \/\/A method for reverse\n   public static void reverseMethod(int number) {\n       if (number &lt; 10) {\n\t   System.out.println(number);\n\t   return;\n       }\n       else {\n           System.out.print(number % 10);\n           \/\/Method is calling itself: recursion\n           reverseMethod(number\/10);\n       }\n   }\n   public static void main(String args&#91;])\n   {\n\tint num=0;\n\tSystem.out.println(\"Input your number and press enter: \");\n\tScanner in = new Scanner(System.in);\n\tnum = in.nextInt();\n\tSystem.out.print(\"Reverse of the input number is:\");\n\treverseMethod(num);\n\tSystem.out.println();\n   }\n}<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Input your number and press enter: \n5678901\nReverse of the input number is:1098765<\/code><\/pre>\n\n\n\n<p><strong>Example: Reverse an already initialized number<\/strong><br>In all the above programs we are prompting user for the input number, however if do not want the user interaction part and want to reverse an initialized number then this is how you can do it.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class ReverseNumberDemo\n{\n   public static void main(String args&#91;])\n   {\n      int num=123456789;\n      int reversenum =0;\n      while( num != 0 )\n      {\n          reversenum = reversenum * 10;\n          reversenum = reversenum + num%10;\n          num = num\/10;\n      }\n\n      System.out.println(\"Reverse of specified number is: \"+reversenum);\n   }\n}<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Reverse of specified number is: 987654321<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Program 1: Reverse a number using while Loop In this program, we are taking the input number from the user using&nbsp;Scanner&nbsp;class and then we are reversing the number using while loop. The logic we are using here is: Inside the\u00a0while loop\u00a0we are dividing the given number by 10 using\u00a0% operator\u00a0and then storing the\u00a0remainder\u00a0in the\u00a0reversenum\u00a0variable after [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[496],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1398"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=1398"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1398\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1398"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1398"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1398"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}