Akashic Records

Top 5 Java recursion examples 본문

Library

Top 5 Java recursion examples

Andrew's Akashic Records 2021. 1. 22. 11:58
728x90
  1. Print a series of numbers with recursive Java methods
  2. Sum a series of numbers with Java recursion
  3. Calculate a factorial in Java with recursion
  4. Print the Fibonacci series with Java and recursion
  5. A recursive Java palindrome checker

1. A simple Java recursion example

A simple program is always the best place to start when you learn a new concept. This first Java recursion example simply prints out the digits from one to the selected number in reverse order.

package com.mcnz.recursion;
public class VerySimpleRecursionExample {
  public static void main(String[] args) {
    callMyself(9);
  }
  /* The recursive Java method */
  public static void callMyself(long i) {
    if (i < 0) {
      return;
    }
    System.out.print(i);
    i = i - 1;
    callMyself(i);
  }
}

2. Sum numbers with recursive Java example

The recursive Java logic is as follows. Start with a number and then add that number to one less than itself. Repeat that logic until you hit zero. Once zero is encountered, the total sum of all numbers from the starting number down to zero has been calculated.

package com.mcnz.recursion;
public class RecursiveSumOfAllNumbers {
  public static void main(String[] args) {
    long sumOfAllNumbers = sumOfAllNumbers(9);
    System.out.println(sumOfAllNumbers);
  }
  /* A recursive Java example to sum all natural numbers up to a given long. */
  public static long sumOfAllNumbers(long number) {
  /* Stop the recursive Java program at zero */
    if (number != 0) {
      return number + sumOfAllNumbers(number - 1);
    } else {
      return number;
    }
  }
}

3. Recursive Java factorial program

package com.mcnz.recursion;
public class RecursiveJavaFactorialProgram {
  public static void main(String args[]) {
    long nFactoriral = factorialProgram(9);
    System.out.println(nFactoriral);
  }
  /* Java factorial program with recursion. */
  public static long factorialProgram(long n) {
    if (n <= 1) {
      return 1;
    } else {
      return n * factorialProgram(n - 1);
    }
  }
}

 

4. Recursive Fibonacci series in Java example

The most common assignment to task young developers learning about recursion is to calculate the Fibonacci series in both iterative and recursive Java programs.

package com.mcnz.recursion;
public class RecursiveFibonnaciJavaProgram {
  public static void main (String args[]) {
    for(long i=1; i<=9; i++){ 
      System.out.print(fibonacci(i) +" "); 
    }
    System.out.println();
  }
  /* A recursive Fibonnaci sequence in Java program */
  public static long fibonacci(long number) {
    if (number == 1 || number == 2) {
      return 1;
    }
    return fibonacci(number - 1) + fibonacci(number - 2);
  }
}

 

5. Recursive Java palindrome program

All of the Java recursion examples so far deal with numbers. The recursive Java palindrome checker program deals with strings, namely to see if a string is spelled the exact same way when the letters in the word are reversed.

package com.mcnz.recursion;
public class JavaPalindromeCheckProgram {
  public static void main(String[] args) {
    boolean flag = palindromeCheck("sitonapanotis");
    System.out.println(flag);
    flag = palindromeCheck("nine");
    System.out.println(flag);
    flag = palindromeCheck("amanaplanacanalpanama");
    System.out.println(flag);
  }
  /* Recursive Java example to check for palindromes */
  public static boolean palindromeCheck(String s){
    if(s.length() == 0 || s.length() == 1) {
      return true;
    }
    if(s.charAt(0) == s.charAt(s.length()-1)) {
      return palindromeCheck(s.substring(1, s.length()-1));
    }
    return false;
  }
}

 

 

728x90

'Library' 카테고리의 다른 글

Xfce, Xubuntu Install and Remove  (0) 2021.02.16
@Secured, @PreAuthorize, @PostAuthorize  (0) 2021.01.27
Docker Compose 설치  (0) 2021.01.07
Lombok features - @With  (0) 2020.12.21
Lombok features - @Synchronized  (0) 2020.12.21
Comments