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

Recursion Code Extensive

Uploaded by

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

Recursion Code Extensive

Uploaded by

barsubiasarah
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import javax.swing.

JOptionPane;

public class Recursion{


public static void main (String[] args) {

String choices []= {"Power", "Factorial", "Fobonacci Sequence", "Min Height


of AVL", "Exit"};
boolean running = true;

while(running){
int choice = JOptionPane.showOptionDialog(null, "Please choose",
"Recursion Practice", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE,
null, choices, choices[0]);
switch(choice){
case 0:
int x = Integer.parseInt(JOptionPane.showInputDialog("Enter
Base Number"));
int y = Integer.parseInt(JOptionPane.showInputDialog("Enter
Exponent Number"));
JOptionPane.showMessageDialog(null, power(x,y));
break;

case 1:
int z = Integer.parseInt(JOptionPane.showInputDialog("Enter
Factorial Number"));
JOptionPane.showMessageDialog(null, factorial(z));
break;

case 2:
int a = Integer.parseInt(JOptionPane.showInputDialog("Enter
Fibonacci Number"));
JOptionPane.showMessageDialog(null, Fibonacci_Sequence(a));
break;

case 3:
int b = Integer.parseInt(JOptionPane.showInputDialog("Enter
Fibonacci Number"));
JOptionPane.showMessageDialog(null, Height(b));
break;

case 4:
int exit = JOptionPane.showConfirmDialog(null, "Are you sure
you want to exit?", "Exit", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
if(exit == JOptionPane.YES_OPTION){
JOptionPane.showMessageDialog(null, "Closing Program...");
running = false;
}
break;
}
}
}

public static int Fibonacci_Sequence(int x){


if(x <= 1) return x;
return Fibonacci_Sequence(x-1) + Fibonacci_Sequence(x-2);
}

public static int power(int base, int pow){


if(pow == 0) return 1;
return base * power(base, pow - 1);
}

public static int factorial(int n){


if(n == 0) return 1;
return n * factorial(n-1);
}

public static int Height(int n){


if (n == 0) return 1;
if (n == 1) return 2;
return Height(n-1) + Height(n-2) + 1;
}
}

You might also like