Create a new Java class called: Fibonacci Write a program that prints out the nt
ID: 3594925 • Letter: C
Question
Create a new Java class called: Fibonacci Write a program that prints out the nth value in the Fibonacci sequence In mathematics, the Fibonacci numbers are the numbers in the following integer sequence 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, The first two numbers in the sequence are 1. Starting with the third number, each number in the sequence is computed by adding the previous two numbers The program should begin by asking the user to enter an integer, n. Afterward, the program should print out the nth value in the Fibonacci sequence rogram runs. The The following is an example of what your MIGHT see on the screen when your p exact output depends on what values that the user types in while the program runs. The user's inputted values are shown below in italics Enter an integer (1 - 46) 1 The 1st number in the Fibonacci sequence is: 1 Here is another example program run Enter an integer (1 - 46): 2 The 2na number in the Fibonacci sequence 131 Here is another example program run Enter an integer (1 - 46):3 The 3rd number in the Fibonacci sequence is:2 Here is another example program run Enter an integer (1 - 46) 6 The 6th number in the Fibonacci sequence is: 8 Here is another example program run Enter an integer (1- 46): 10 The 10th number in the Fibonacci sequence is: 55Explanation / Answer
/**
* The java program Fibonacci that prompts user to enter
* an input number and print nth fibonacci number
* */
//Fibonacci.java
import java.util.Scanner;
public class Fibonacci {
public static void main(String[] args) {
//Create an instance of Scanner class
Scanner scanner=new Scanner(System.in);
int n=0,input;
System.out.printf("Enter an integer (1-46) :");
//prompt for integer value and convert to ineger
input=scanner.nextInt();
if(input<0)
System.out.println("Not a valid number.");
else
{
n=getNFibonacci(input);
//checking if input is 11,12 or 13
//print th in output
if(input==11||input==12||input==13)
System.out.printf("The %dth number in the Fibonacci sesquence is : %d",input,n);
//checking if input is 1
else if(input==1 )
//print st in output
System.out.printf("The %dst number in the Fibonacci sesquence is : %d",input,n);
//checking if input is 2
else if(input==2)
//print nd in output
System.out.printf("The %dnd number in the Fibonacci sesquence is : %d",input,n);
//checking if input is 3
else if(input==3)
//print rd in output
System.out.printf("The %drd number in the Fibonacci sesquence is : %d",input,n);
else
System.out.printf("The %dth number in the Fibonacci sesquence is : %d",input,n);
}
}
/**
* The method getNFibonacci takes an integer value
* and returns the nth fibonacci number in the series
* */
private static int getNFibonacci(int n)
{
int sum = 0;
if(n==0)
return 0;
else if(n==1)
return 1;
else
{
int a=0;
int b=1;
//for loop to fibonacci number
for (int i = 2; i <=n; i++)
{
sum=a+b;
a=b;
b=sum;
}
}
//retun c
return sum;
}
}//end of class
----------------------------------------------------------------------------------------------------
Sample output:
Enter an integer (1-46) :1
The 1st number in the Fibonacci sesquence is : 1
Enter an integer (1-46) :2
The 2nd number in the Fibonacci sesquence is : 1
Enter an integer (1-46) :3
The 3rd number in the Fibonacci sesquence is : 2
Enter an integer (1-46) :6
The 6th number in the Fibonacci sesquence is : 8
Enter an integer (1-46) :10
The 10th number in the Fibonacci sesquence is : 55
Enter an integer (1-46) :-1
Not a valid number.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.