0% found this document useful (0 votes)
1 views

common_recursion

The document contains several recursive Java methods for common programming tasks, including calculating Fibonacci numbers, finding the maximum value in an array, computing the average of array elements, reversing a string, checking if a string is a palindrome, and converting a decimal number to binary. Each method is defined with base cases and recursive calls to achieve the desired functionality. The document serves as a reference for implementing recursive algorithms in Java.

Uploaded by

lulwah.loly
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

common_recursion

The document contains several recursive Java methods for common programming tasks, including calculating Fibonacci numbers, finding the maximum value in an array, computing the average of array elements, reversing a string, checking if a string is a palindrome, and converting a decimal number to binary. Each method is defined with base cases and recursive calls to achieve the desired functionality. The document serves as a reference for implementing recursive algorithms in Java.

Uploaded by

lulwah.loly
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

FIB:

public static int fib(int n){

if (n <= 2){

return 1;

return fib(n-1) + fib(n-2);

MAX

public static int arrayMax_recur(int[] arr, int start) {

if (arr.length - 1 == start) {

return arr[start];

return Math.max(arr[start], arrayMax_recur(arr, start + 1));

FIND AVG:

public static double findAverage(int[] arr, int start) {

if (start == arr.length - 1) {

return arr[start];

} else if (start == 0) {

return ((arr[start] + findAverage(arr, start + 1)) / arr.length);

return (arr[start] + findAverage(arr, start + 1));

}
REVERSE

public static String rev(String S) {

if (S.equals("")) {

return "";

//AB

return rev(S.substring(1)) + S.charAt(0);

ISPAL

public static boolean isPal(String s) {

if (s.length() == 0 || s.length() == 1) {

return true;

if (s.charAt(0) == s.charAt(s.length() - 1)) {

return isPal(s.substring(1, s.length() - 1));

return false;

DECtoBIN

public static String decToBin(int num) {

if (num == 0) {

return "";

int rem = num % 2;

return decToBin(num / 2) + rem;

}
Num ways

If n == 0 or n == 1

Return 1;

Return num ways (n-1) + numWays(n-2)

You might also like