The n^th Fibonacci number F_n is defined as follows F_0 = 1, F_1 = 1, and F_n =
ID: 3846642 • Letter: T
Question
The n^th Fibonacci number F_n is defined as follows F_0 = 1, F_1 = 1, and F_n = F_n-1 + F_n-2 for n > 1 In other words, each number is the sum of the two previous numbers in the sequence. Thus, the first several Fibonacci numbers are 1, 1, 2, 3, 5, and 8. The input to the program from the keyboard are two initial values in a sequence and the number of Fibonacci values to print. The program print the computed sequence of the Fibonacci values (including the first two initial values) in groups of five values in a row. Namely, if you generated 12 Fibonacci values, two rows of five values each followed by two values will be printed Each value is printed taking up 6 spaces (right-justified).Explanation / Answer
Since you haven't mention the prrogramming language in which you want the solution,I am providing you the solution in Java Language.
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Chegg
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc=new Scanner(System.in);//Scanner object to take user input
//To take f1,f2 and no. of fibonacci values to print in variable n from keyboard
int f1=sc.nextInt();
int f2=sc.nextInt();
int n=sc.nextInt();//no. of fibonacci values to print in variable n
System.out.print(f1+" "+f2+" ");//first two values are printed first
int res;//res contain the summation of f1 and f2
for(int i=3;i<=n;i++)//loop from 3 to n and not from 1 to n because first 2 values are already printed
{
res=f1+f2;//stores the summation of f1 and f2
System.out.print(res+" ");//printing the value res
//Now the values will be exchanged
f1=f2;
f2=res;
if(i%5==0)//After 5 values is printed ,values should get printed in next row
System.out.println();
}
}
}
Sample Input:
1 1 12
Sample Output: