1) The greatest common divisor of integers x and y is the largest integer that e
ID: 3573372 • Letter: 1
Question
1) The greatest common divisor of integers x and y is the largest integer that evenly divides both x and y. Write a recursive function gcd that returns the greatest common divisor of x and y. The gcd of x and y is defined recursively as follows. If y is 0 the answer is x; otherwise gcd(x, y) is gcd(y, x%y). 2) Write a recursive program that finds out if n is a Fibonacci number or not. The Fibonacci numbers are 0 1 1 2 3 5 8 13 21 … Your program should have the following interface: Enter a number (enter a negative number to quit): 14 !!!!! Sorry14 is not a Fibonacci number Enter a number (enter a negative number to quit): 13 Yes, you got it, 13 is a Fibonacci number Enter a number (enter a negative number to quit): 22 !!!!! Sorry 22 is not a Fibonacci number Enter a number (enter a negative number to quit): 23 !!!!! Sorry 23 is not a Fibonacci number Enter a number (enter a negative number to quit): 21 Yes, you got it, 21 is a Fibonacci number Enter a number (enter a negative number to quit): -1 (*Thanks – Have a good Day*)
Explanation / Answer
Q1)import java.util.*;
public class gcd {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
System.out.println("enter fistvalue:");
int first=sc.nextInt();
System.out.println("enter second:");
int second=sc.nextInt();
gcd g=new gcd();
System.out.println(g.gcd1(first, second));
}
public int gcd1(int first,int second){
int first1=first;
int second1=second;
if(second1==0)
return first1;
else if(second1>first1)
return gcd1(second1,first1);
else
return gcd1(second1,first1%second1);
}
}
output:
enter fistvalue:
81
enter second:
153
9
2Q0)
import java.util.*;
public class fibnoic_number {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
while(true){
System.out.println("enter a number to check it is febnoic or not:");
System.out.println("febnoic numbers are 0 1 1 2 3 5 8 13 21 ");
System.out.println("enter negitive number to quit:");
int n=sc.nextInt();
if(n<0)
System.exit(0);
else {
int val=fibnocy(n);
System.out.println(val+" ");
if(n==0||n==1||n==2||n==3||n==5||n==8||n==13||n==21)
System.out.println("yes you got is "+n+" is fibnoci");
else
System.out.println("sorry"+n +"is not febonic");
}
}
}
public static int fibnocy(int n){
if(n == 1 || n == 2){
return 1;
}
return fibnocy(n-1) + fibnocy(n -2);
}
}
enter a number to check it is febnoic or not:
febnoic numbers are 0 1 1 2 3 5 8 13 21
enter negitive number to quit:
5
5
yes you got is 5 is fibnoci
enter a number to check it is febnoic or not:
febnoic numbers are 0 1 1 2 3 5 8 13 21
enter negitive number to quit:
13
233
yes you got is 13 is fibnoci
enter a number to check it is febnoic or not:
febnoic numbers are 0 1 1 2 3 5 8 13 21
enter negitive number to quit:
21
10946
yes you got is 21 is fibnoci
enter a number to check it is febnoic or not:
febnoic numbers are 0 1 1 2 3 5 8 13 21
enter negitive number to quit:
34
5702887
sorry34is not febonic
enter a number to check it is febnoic or not:
febnoic numbers are 0 1 1 2 3 5 8 13 21
enter negitive number to quit:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.