Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please use java to finish this program. Exercise 1: These are recursive programs

ID: 3595952 • Letter: P

Question

Please use java to finish this program.

Exercise 1: These are recursive programs discussed in the lectures. a) Fibonacci series 0,1,1,2,3,5,8,13,21,34, ifn-0, then fib(n)=0 //base case if n--1, then fib(n)-1 //base case if n> I, then fib(n) = fib(n-l ) + fib(n-2) Write a recursive method public static int fib(int n) that finds the nth number in the Fibonacci series Use this method in the main method to print the first 20 numbers in the Fibonacci series. b) Factorial of an integer n if n-0, then factorial(n)= 1 if n > 0, then factorial(n) = n * factorial(n-1) Write a recursive method public static int factorial (int n) that finds factorial of a non-negative integer n. Use this method in the main method to print the factorials of 1 to 10. x to the power n if n0, then power(x,n)1 if n>0, then power(x,n) = power(x,n-1)*x c) Write a recursive method public static int power(int x, int n) that finds the x to the power n, where x and n are positive integers. Use this method in the main method that prompts the user to enter x and n and prints x to the power of n.

Explanation / Answer

a) Fibonacci Series

import java.util.*;
import java.util.Scanner;
public class fibonacci
{
public static void main(String aregs[])
{
Scanner scan = new Scanner(System.in);
System.out.println(" Enter number to print Fibonacci series: ");
int number = scan.nextInt();
System.out.println(" Fibonacci series upto " + number + ".");
for(int i = 0; i <= number; i++)
{
System.out.println(fib(i));
}
}
public static int fib(int n)
{
if(n == 0)
return 0;
if(n == 1)
return 1;
else
return fib(n - 1) + fib(n - 2);
}
}

OUTPUT


Enter number to print Fibonacci series: 0

Fibonacci series upto 0.
0


Enter number to print Fibonacci series: 1

Fibonacci series upto 1.
0
1


Enter number to print Fibonacci series: 20

Fibonacci series upto 20.
0
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765

b) Factorial

import java.util.*;
import java.util.Scanner;
public class fact
{
public static void main(String aregs[])
{
Scanner scan = new Scanner(System.in);
System.out.println(" Enter number to find the factorial of a given number: ");
int number = scan.nextInt();
for(int i = 0; i <= number; ++i)
{
System.out.println(factorial(i));
}
}
public static int factorial(int n)
{
if(n == 0)
return 1;
else
return n * factorial(n - 1);
}
}

OUTPUT


Enter number to find the factorial of a given number:
1
1
2
6
24
120

c) x to the power n

import java.util.*;
import java.util.Scanner;
public class pow
{
public static void main(String aregs[])
{
Scanner scan = new Scanner(System.in);
System.out.println(" Enter value number: ");
int number = scan.nextInt();
System.out.println(" Enter value power: ");
int po = scan.nextInt();
System.out.println(power(number, po));
  
}
public static int power(int x, int n)
{
if(n == 0)
return 1;
else
return power(x, n - 1) * x;
}
}

OUTPUT

Enter value number: 2

Enter value power: 2
4


Enter value number: 4

Enter value power: 5
1024