EEC$168 Lat Exercise 3: Fibonacci Detector Create a second Fibonacci program in
ID: 3871008 • Letter: E
Question
EEC$168 Lat Exercise 3: Fibonacci Detector Create a second Fibonacci program in a new folder. This program will allow the user to input a value and verity if it is a number in the fibonacci sequence Example Run: Enter a value: 5 5 is the 6th value in the Fibonacci Sequence! Goodbye Enter a value: 7 7 is not in the Fibonacci sequence Goodbye Example special cases to handle: Enter a value: 1 1 is the 2nd and 3rd values in the Fibonacci Sequence Goodbye Exercise 4: Prime Detector Create a program that takes an int from the user and tells the user whether or not it is prime. This repeats until the user wants to quit. Quiting is indicated by a y' at the prompt. Input an integers 7 7 is prime Do you want to quit (y/n):n Input an integeri 4 4 is not prime Do you want to quit (y/n)i n Input an integers 199 199 ia prine Do you vant to quit (y/n)iy GoodbyeExplanation / Answer
Exercise 3 : Fibonacci Detector
import java.util.Scanner;
public class FibonacciExample1{
public static void main(String args[])
{
int n1=0,n2=1,n3=0,i;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a value:");
int n4=sc.nextInt();
if(n4==1)
{
System.out.println("1 is the 2nd value in the Fibonacci sequence!");
System.out.println("Goodbye");
System.exit(0);
}
if(n4==0)
{
System.out.println("0 is the 1st value in the Fibonacci sequence!");
System.out.println("Goodbye");
System.exit(0);
}
else {
for(i=2;n4>=n3;++i)//loop starts from 2 because 0 and 1 are already printed
{
if(n4==n3){
System.out.println(n4+" is the "+i+"th value in the Fibonacci sequence!");
System.out.println("Goodbye");
System.exit(0);
}
n3=n1+n2;
n1=n2;
n2=n3;
}
if(n4!=n3)
{
System.out.println(n4+" is not in the Fibonacci sequence!");
}
}
}}
Exercise 4: Prime Detector
import java.util.Scanner;
public class PrimeExample{
public static void main(String args[]){
int i,m=0,flag=0;
String ch;
Scanner sc=new Scanner(System.in);
do{
System.out.println("Input a integer:");
int n=sc.nextInt();
m=n/2;
for(i=2;i<=m;i++){
if(n%i==0){
System.out.println(n+" is not prime");
flag=1;
break;
}
}
if(flag==0)
{
System.out.println(n+" is prime");
}
System.out.println("Do you want to quit (y/n) :");
ch = sc.next();
if(ch.equals("y"))
System.exit(0);
}while(ch.equals("n"));
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.