recursion
RECURSION USING STACK
package recursion;
import java.util.Stack;
public class Factorial {
// Recursive factorial function
public static int recursiveFactorial(int n) {
if (n == 0 || n == 1) {
return 1;
} else {
return n * recursiveFactorial(n - 1);
}
}
// Factorial with stack function
public static int factorialWithStack(int n) {
// Create a stack to simulate recursion
Stack<Integer> stack = new Stack<>();
stack.push(n); // Push the initial value onto the stack
int result = 1; // Initialize the result variable to 1
// Iterate until the stack is empty
while (!stack.isEmpty()) {
int num = stack.pop(); // Pop a number from the stack
// Multiply the result by the popped number
result *= num;
// If the popped number is greater than 1, push (num - 1) onto the stack
if (num > 1) {
stack.push(num - 1);
}
}
return result; // Return the factorial result
}
// Main method to test the recursive and stack-based factorial functions
public static void main(String[] args) {
int num = 5; // Number for which factorial is calculated
// Calculate and print factorial using recursion
System.out.println("Factorial of " + num + " using recursion: " + recursiveFactorial(num));
// Calculate and print factorial using stack
System.out.println("Factorial of " + num + " using stack: " + factorialWithStack(num));
}
}
TOWER OF HANOI
package recursion;
import java.util.Stack;
public class TowerOfHanoi {
public static void towerOfHanoi(int numDisks, Stack<Integer> source, Stack<Integer> auxiliary, Stack<Integer> destination) {
if (numDisks == 1) {
destination.push(source.pop());
System.out.println("Move disk 1 from source to destination");
return;
}
towerOfHanoi(numDisks - 1, source, destination, auxiliary);
destination.push(source.pop());
System.out.println("Move disk " + numDisks + " from source to destination");
towerOfHanoi(numDisks - 1, auxiliary, source, destination);
}
public static void main(String[] args) {
int numDisks = 3;
Stack<Integer> source = new Stack<>();
Stack<Integer> auxiliary = new Stack<>();
Stack<Integer> destination = new Stack<>();
// Initialize source stack with disks
for (int i = numDisks; i >= 1; i--) {
source.push(i);
}
System.out.println("Initial configuration:");
System.out.println("Source: " + source);
System.out.println("Auxiliary: " + auxiliary);
System.out.println("Destination: " + destination);
// Solve Tower of Hanoi problem
towerOfHanoi(numDisks, source, auxiliary, destination);
System.out.println("Final configuration:");
System.out.println("Source: " + source);
System.out.println("Auxiliary: " + auxiliary);
System.out.println("Destination: " + destination);
}
}