-1

I'm trying to learn java, and I can't seem to understand recursion. I can understand how recursion can be used to add and do other basic math operations but how can recursion be used to reverse manipulate integers and individual integer digits.

example: a method takes a single positive integer argument and displays its base five equivalent. 231 returns 1411 but the code below returns 1141. how would I reverse the order of integers put out?

public void base5(int n){
    int rem=n%5;
    int vis=n/5;
    if(n!=0){
   //   System.out.print(rem/*+"|"*/);
    // 

// rem=(rem+rem)*10;
       //   System.out.print("\n||"+n+"||\n");
System.out.print(rem);
         base5(vis);
          }
    else{
     
      return;
    }
  } 
DC_Fruit
  • 3
  • 3
  • Reversing recursively is at least somewhat intuitiveI think. `reverse(firstChar, middle, lastChar) = lastChar + reverse(middle) + firstChar` – CollinD Nov 25 '22 at 19:20
  • The general principle for recursively processing a string is that any non-empty string X has a first character, followed by a string Y made up of the rest of string X. (Or variations on that theme). So recursively process Y. – access violation Nov 25 '22 at 19:30
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – marquies Nov 25 '22 at 20:52
  • "how would I reverse the order of integers put out?" Swap the order of your `print` statement and recursive call and see what happens... – Idle_Mind Nov 27 '22 at 15:47

1 Answers1

0

The algorithm for getting individual digits of an integer, from right to left, is well known. See How to get the separate digits of an int number?.

I won't "explain" recursion, but I'll give you one possible solution for first problem:

a method takes a single positive integer and displays it with commas inserted every three digits

import java.util.Scanner;
class Main {

  public static void main( String [] args) {
    Scanner sc = new Scanner(System.in);
    
    System.out.print("Enter your positive integer: ");
    long number = sc.nextLong();    
    String result = addCommas(number);
    System.out.println(result);
  } 

  public static String addCommas(long num) {
    return addCommas(num, 1);
  }
  
  public static String addCommas(long num, int counter) {
    if (num == 0) {
      return ""; // base case ends recursion
    }
    else {
      long digit = num % 10;      
      num = num / 10;
      String comma = (counter%3==0 && num>0) ? "," : "";
      // recursive call below because we call addCommas() again
      return addCommas(num, counter+1) + comma + digit;
    }    
  }
  
}

Here's a compact solution to the second problem:

a method takes a single positive integer and displays the result of reversing its digits

  public static String reverseDigits(long num) {
    if (num == 0) { 
      return "";
    }
    else {
      return String.valueOf(num % 10) + reverseDigits(num / 10);
    }
  }
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40