Question

In: Computer Science

There are many applications of the Fibonacci series both in mathematics and in the real world....

There are many applications of the Fibonacci series both in mathematics and in the real world. The Fibonacci series is obtained by starting with 0 and 1, and each subsequent term in the series is obtained by adding the previous two terms. Given n in the top row of the table below, the numbers in the second row represent Fib(n), the numbers in the Fibonacci series. So Fib(0) = 0, and Fib(1) = 1. Then Fib(2)is obtained by adding Fib(0)and Fib(1), or 0 + 1 = 1. n 0 1 2 3 4 5 6 7 8 9 10 Fib(n)0 1 1 2 3 5 8 13 21 34 55 Write a program for each value of n, print the Fibonacci terms that add up to it. Let the user input a single positive number n (0 < n < 1000). Error check input size. The Fibonacci terms shall be in descending order and not use two successive terms in the Fibonacci series. Finally, the program should ask if the user wants to run the program again (Check case). Use either recursion or iteration to solve this problem. Refer to the sample output below.Sample Run: Enter n: 1716 = 13 + 3 + 1Run again (Y/N): yEnter n: 5353 = 34 + 13 + 5 + 1Run again (Y/N): yEnter n: 9292 = 89 + 3Run again (Y/N): NName the program: FiboTermsXX.java or FiboTermsXX.cpp, where XX are your initials

Solutions

Expert Solution

Here is the code:

# include <iostream>
using namespace std;

//function to convert to lower case
char toLower(char c) {
//if upper case, then subtract A and add a
//to get lowercase
if (c <= 'Z' && c >= 'A') return c - 'A' + 'a';
return c;
}

int main() {
//calculate the Fibonacci numbers first
//note that at most Fib(20) is needed for
//n upto 1000
int Fib[21];
Fib[0] = 0;
Fib[1] = 1;
for (int i=2; i<21; i++) {
Fib[i] = Fib[i-1] + Fib[i-2];
}

int n;
//take input till user says no
while (true) {
cout << "Enter n: ";
cin >> n;
//ask again if out of range
while (n < 1 || n >= 1000) {
cout << "Please enter a number more than 0 and less than 1000" << endl;
cout << "Enter n: ";
cin >> n;
}
//use the Fib array to get the required sum
cout << n << " = ";
for (int i=20; i>0 && n>0; i--) {
//if Fib(i) is at most n
if (Fib[i] <= n) {
//then use it for the sum
cout << Fib[i];
n -= Fib[i];
//if n is still more than 0, then more terms are needed
if (n > 0) cout << " + ";
}
}
cout << endl;

//check if user wants another input
char yN;
cout << "Run again(Y/N): ";
cin >> yN;
//convert to lower case
yN = toLower(yN);
if (yN == 'n') break;
}
return 0;
}

Here is a screenshot of the code:

Here is a screenshot of the output of the code:

Comment in case of any doubts.


Related Solutions

What extensions to other mathematics and real world applications does Group Cohomology have?
What extensions to other mathematics and real world applications does Group Cohomology have?
real-world applications of calibration of pressure gauge  
real-world applications of calibration of pressure gauge  
What real world applications exist for Euler Circuits?
What real world applications exist for Euler Circuits?
The concept of time value of money has numerous "real-world" applications. Some of the applications range...
The concept of time value of money has numerous "real-world" applications. Some of the applications range from calculating the payment for a car or mortgage to estimating what interest rate is needed on an investment to send your child to college in 20 years. In your discussion, respond to the following two questions: Do you believe the concept of time value money is important in ordinary business relationships? Explain. How would you use a concept of time value to determine...
Respond to the following in a minimum of 175 words: Explain the real world applications of...
Respond to the following in a minimum of 175 words: Explain the real world applications of Statistics and provide a detailed example.
The objective of this experiment is to identify real world applications of quantum mechanics. Write a...
The objective of this experiment is to identify real world applications of quantum mechanics. Write a paper with a minimum of 1200 words discussing real world applications of quantum mechanics. Do NOT copy and paste off the internet!
What role does new technology play in creating new applications? Cite real world applications.
What role does new technology play in creating new applications? Cite real world applications.
The Fibonacci sequence is an infinite sequence of numbers that have important consequences for theoretical mathematics...
The Fibonacci sequence is an infinite sequence of numbers that have important consequences for theoretical mathematics and applications to arrangement of flower petals, population growth of rabbits, and genetics. For each natural number n ≥ 1, the nth Fibonacci number fn is defined inductively by f1 = 1, f2 = 2, and fn+2 = fn+1 + fn (a) Compute the first 8 Fibonacci numbers f1, · · · , f8. (b) Show that for all natural numbers n, if α...
Python: Using Jupyter Notebook 1. Write code to generate Fibonacci series. Fibonacci numbers – 1, 1,...
Python: Using Jupyter Notebook 1. Write code to generate Fibonacci series. Fibonacci numbers – 1, 1, 2, 3, 5, 8, … 2. Check if a number is an Armstrong number A positive integer is called an Armstrong number of order n if abcd... = a^n + b^n + c^n + d^n + ... In case of an Armstrong number of 3 digits, the sum of cubes of each digits is equal to the number itself. For example: 153 = 1*1*1...
The Fibonacci Sequence is a series of integers. The first two numbers in the sequence are...
The Fibonacci Sequence is a series of integers. The first two numbers in the sequence are both 1; after that, each number is the sum of the preceding two numbers. 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ... For example, 1+1=2, 1+2=3, 2+3=5, 3+5=8, etc. The nth Fibonacci number is the nth number in this sequence, so for example fibonacci(1)=1, fibonacci(2)=1, fibonacci(3)=2, fibonacci(4)=3, etc. Do not use zero-based counting; fibonacci(4)is 3, not 5. Your assignment...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT