/home/caleb/ASDV-Java/Semester 3/Assignments/RecursionDemo/src/main/java/edu/slcc/asdv/caleb/recursiondemo/RecursionDemo.java |
nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt
package edu.slcc.asdv.caleb.recursiondemo;
public class RecursionDemo {
static void printNTimes(int nTimes, String message) {
for (int i = 0; i < nTimes; ++i) {
System.out.println(message);
}
}
public static boolean isPalendrome(String s) {
String backwards = "";
for (int i = s.length() - 1; i >= 0; i--) {
backwards += s.charAt(i);
}
return s.equals(backwards);
}
public static long fib(int n) {
if (n == 1 || n == 2) {
return 1;
}
System.out.println(n);
return fib(n - 1) + fib(n - 2);
}
static void printNTimesRecursion(int nTimes, String message) {
if (nTimes == 0) {
return;
}
System.out.println(message);
printNTimesRecursion(--nTimes, message);
}
public static boolean isPalendromeRecursion(String s) {
if (s.length() == 0 || s.length() == 1) {
return true;
} else if (s.charAt(0) != s.charAt(s.length() - 1)) {
return false;
}
return isPalendromeRecursion(s.substring(1, s.length() - 1));
}
public static long factorial(int n) {
long fact;
if (n == 1) {
return 1;
}
fact = n * factorial(n - 1);
return fact;
}
public static void printArrayRecursively(int[] arr, int index) {
if (index == arr.length) {
return;
} else {
System.out.print(arr[index] + " ");
printArrayRecursively(arr, index + 1);
}
}
arr
index
currentMax
public static int findMaximumRecursive(int arr[], int index, int currentMax) throws Exception {
if (index >= (arr.length)) {
return currentMax;
}
if (arr[index] >= currentMax) {
currentMax = arr[index];
}
currentMax = findMaximumRecursive(arr, (index + 1), currentMax);
return currentMax;
}
public static int findMaximumRecursive(int arr[]) throws Exception {
return findMaximumRecursive(arr, 0, Integer.MIN_VALUE);
}
public static void main(String[] args) throws Exception {
System.out.println(findMaximumRecursive(new int[]{99, 1, 44, 2, 3, 55}));
System.out.println(findMaximumRecursive(new int[]{1, 44, 2, 3}));
System.out.println(findMaximumRecursive(new int[]{1, 44, 2, 3, 300}));
}
}