Write the class named CRecursion that includes allthe static methods the followi
ID: 3617801 • Letter: W
Question
Write the class named CRecursion that includes allthe static methods the following problemsby using recursive algorithm:
Write the class named Tester that will display the menu:
1. Factorial
2. Power
3. Sum
4. Fibonacci
5. GCD
Choose 1 to 5 to continue, 0 tostop
Read the answer based on selected cases:
· For case1: read n input then display the result
· For case2: read input n and base x, then display the result
· For case3: read n, then display the result
· For case4: read n, then display the result
· For case5: n=120, m=300, then display the result
Explanation / Answer
please rate - thanks import java.util.*; public class CRecursion {public static void main(String [] args) {Scanner in=new Scanner(System.in); int num1,num2; int choice; for(;;) {System.out.println("1. Factorial"); System.out.println("2. Power"); System.out.println("3. Sum"); System.out.println("4. Fibonacci"); System.out.println("5. GCD"); System.out.print("Choose 1 to 5 to continue, 0 tostop: "); choice=in.nextInt(); switch(choice) {case 0: System.exit(0); case 1:System.out.print("enter a number : "); num1=in.nextInt(); System.out.println(num1+"!="+factorial(num1)); break; case 2: System.out.print("enter a number whose power you want to find:"); num1=in.nextInt(); System.out.print("Enterthe exponent: "); num2=in.nextInt(); System.out.println(num1+"^"+num2+" = "+power(num1,num2)); break; case 3: System.out.print("enter a number : "); num1=in.nextInt(); System.out.print("Enteranother, larger, number: "); num2=in.nextInt(); System.out.println("The sum of the numbers from "+num1+" to"+num2+" = "+sum(num1,num2)); break; case 4: System.out.print("enter a number : "); num1=in.nextInt(); System.out.println("The "+num1+"th fibonacci number is"+fib(num1)); break; case 5: System.out.print("enter a number : "); num1=in.nextInt(); System.out.print("Enteranother number: "); num2=in.nextInt(); System.out.println("TheGCD of "+ num1+" and "+num2+" = "+gcd(num1,num2)); break; } } } public static int sum(int a,int b) {if(b==a) return a; else return sum(a,b-1)+b; } public static double power( double num1, int num2) { if(num2Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.