//Done by Mirsad Capric and Hoi-Neng Ip
import java.io.*;

public class RecursionTest 
{ 
	//Method for factorial
	public static long fact ( int n )  throws IOException
	{
           if (1 == n || 0 == n)
                 return 1;
           else
                 return (n * fact(n - 1));
 	}

	//Method for fibonaci
	public static int fib ( int n )  throws IOException
	{
           if ( n == 1 || n == 2 )
                 return 1;
           else
                 return ( fib (n - 2) + fib (n - 1) );
     }

	//Method for addition
	public static int add (int a, int b) throws IOException
	{
		if (b == 0) 
			return a;
		else
			return add (a, b-1) + 1;
	}

	//method for the first gcd, which is in the handout
   public static int gcd1 (int a, int b) throws IOException
   {
		 if (b == 0)
			return a;
		else 
			return gcd1 (b, a % b);
   }
	
	//method for the second gcd, which was done in class
	public static int gcd2 (int a, int b) throws IOException
	{
		if (a == b)
      		 return a;
		else if (a > b)
			 return gcd2 (a - b, b);
		else
			 return gcd2 (a, b - a);
    }

	//method for H
	public static int H(int n) throws IOException
	
   	{
   	   	if (n == 1)
			return 0;
	
		else if ((n>1) && (n%2!=0))
			return H(3*n+1);

		else if (n%2==0)
			return 1+H(n/2);

		else return H(n);	
   	 }
  
  //method for M
  public static int M(int n) throws IOException
   {
   	
   		if(n > 100)
   			 return n-10;
   		else
   			return M(M (n + 11));
   		
   }
   		
	public static void main (String[] args) throws IOException
	{   
			//Lets user select which fucntion they would like to do
			BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
		  	System.out.println("Choose a function: "); //Input the selection, from 1-7
			System.out.println("1 for factorial"); //Factorial
			System.out.println("2 for fibonacci"); //Fibonacci
			System.out.println("3 for addition"); //addition
			System.out.println("4 for gcd1 (the one in the notes)");  //First gcd
			System.out.println("5 for gcd2 (the one done in class"); //second gcd
			System.out.println("6 for H"); // H
			System.out.println("7 for M"); //M
			int x=Integer.parseInt(input.readLine());		//Reads in the input
			int y=0;
			
				
		switch(x)
		 {	
		 	//Factorial
		    case 1:
		    {
		      try
		      {
			  System.out.println("Enter a number that is equal to or greater than 0: "); //Must enter a natural number
		      x= Integer.parseInt(input.readLine());	//reads in the input (n) from the user
		      System.out.println("The answer is: " + fact(x)); //Displays answer
		      } catch (NumberFormatException e) 
		      	{System.out.println("Enter a number that is equal to or greater than 0: "); } //Displays error
		      break;
		    }			    
			
			//Fibonacci    
		    case 2:
		    {
			  try
			  {
		      System.out.println("Enter a number that is equal to or greater than 0: "); //Must enter a natural number
		      x= Integer.parseInt(input.readLine());	//Reads in input (n) from user
		      System.out.println("The answer is:" +fib(x)); //Displays answer
		      } catch (NumberFormatException e) 
		      {System.out.println("Enter a number that is equal to or greater than 0: "); } //Displays error
			   break;
		    }
		    
		    //Addition
		    case 3:
		    {
			 try
			 {
			  System.out.println("Enter a number that is equal to or greater than 0: "); //Must enter a natural number
		      x = Integer.parseInt(input.readLine()); //Inputs a for addition
		      System.out.println("Enter a number that is equal to or greater than 0: ");	
		      y = Integer.parseInt(input.readLine()); //Inputs b for addition
		      System.out.println("The answer is:"+ add(x,y)); //Displays answer
		      } catch (NumberFormatException e) 
		      {System.out.println("Enter a number that is equal to or greater than 0: "); }	 //Displays error
		      break;				   
		    }
			    
			//GCD1
		    case 4:
		    {
			 try
			 {
			  System.out.println("Enter a number that is equal to or greater than 0: "); //Must enter a natural number
		      x = Integer.parseInt(input.readLine()); //Reads in a for gcd1
		      System.out.println("Enter a number that is equal to or greater than 0: ");	
		      y = Integer.parseInt(input.readLine()); //Reads in b for gcd1
		      System.out.println("The answer is: "+ gcd1(x,y)); //Displays answer
		      } catch (NumberFormatException e) 
		      {System.out.println("Enter a number that is equal to or greater than 0: "); }	 //Displays error
		      break;	
		    }
			   
		   //GCD2
		   case 5:
		    {
			 try
			 {
			  System.out.println("Enter a number that is equal to or greater than 0: "); //Must enter a natural number
		      x = Integer.parseInt(input.readLine()); //Reads in a for gcd2
		      System.out.println("Enter a number that is equal to or greater than 0: "); 
 		      y = Integer.parseInt(input.readLine()); //Reads in b for gcd2
		      System.out.println("The answer is: "+ gcd2(x,y)); //Displays answer
		      } catch (NumberFormatException e) 
		      {System.out.println("Enter a number that is equal to or greater than 0: "); }	//Displays error
		      break;	
		    }
		    
		    //H
		    case 6:
		    {
			 try
			 {
			  System.out.println("Enter a number that is equal to or greater than 0: "); 
		      x = Integer.parseInt(input.readLine()); //Reads in n for H
		      System.out.println("The answer is: "+ H(x)); //Displays answer
		      } catch (NumberFormatException e) 
		      {System.out.println("Enter a number that is equal to or greater than 0: "); } //Displays error
		      break;
			}
		    
			 //M		    
		     case 7:
		    {
			 try
			 {
			  System.out.println("Enter a number that is equal to or greater than 0: "); 
		      x = Integer.parseInt(input.readLine()); //Reads in n for M
		      System.out.println("The answer is: "+ M(x)); //Displays answer
		     } catch (NumberFormatException e) 
		     {System.out.println("Enter a number that is equal to or greater than 0: "); } //Displays error
		       break;
			}
	    	
		} 
	     
	}
}

  	
  	