Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Create a new Java class called: Fibonacci Write a program that prints out the n th value in the Fibonacci sequence. In mathematics, the Fibonacci

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.

The following is an example of what your MIGHT see on the screen when your program runs. The 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 2nd number in the Fibonacci sequence is: 1

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: 55

Here is another example program run:

Enter an integer (1 - 46): -5 Not a valid number.

Here is another example program run:

Enter an integer (1 - 46): 47 Not a valid number.

Technical Notes & Requirements:

You must print the output as shown in the examples above.

You must print the correct suffix behind the user's number in the final output. Any number ending with 1 should be followed by "st". Numbers ending in 2 should be followed by "nd". Numbers ending in 3 should be followed by "rd", all other numbers should be followed by "th". The exception to this rule would be numbers 11, 12 and 13 which should be followed by "th".

If the user enters a number below 1 or above 46, your program should print a message that the number is not valid, and then stop.

If the user enters 1 or 2, your program can just simply print out the answer "1", and will not need to compute anything.

If the user enters a number above 2, then you will need to compute the value of the nth Fibonacci number by using a loop.

Refer to the following website for a Fibonacci calculator that you can use to verify the answers that your program produces: http://php.bubble.ro/fibonacci/

There is no need to attempt to store all of the Fibonacci numbers. You only need the last two values to compute the next one in the series: fold1 = 1; fold2 = 1; fnew = fold1 + fold2;

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Graph Databases New Opportunities For Connected Data

Authors: Ian Robinson, Jim Webber, Emil Eifrem

2nd Edition

1491930896, 978-1491930892

Students also viewed these Databases questions

Question

6. The cost of the training creates a need to show that it works.

Answered: 1 week ago