Write a MARIE program to calculate Fib(n), where the user inputs n. For example, if the user inputs 7, the program outputs the value 13; if the user inputs 15, the program outputs the value 610; if the user inputs 20, the program outputs the value 6765 and so on. You need to write and run the program using MARIE simulator. Please include appropriate comments to make your code readable.[8 marks]
Answers
The program is written in java:-
import java.io.*;
class fibonacci
{
public static void main(String args[])throws IOException
{
Buffered Reader in=new Buffered Reader (new Inut Stream Reader ( System . in ) );
System . out . println (" Enter the number ");
int n=Integer. parse Int ( in. readLine);
int a = 1, b = 1;
int sum=0;
for( int i=0 ; i<=n; i++)
{
sum =a+b;
System .out. println ( sum );
a = b;
b = sum;
}
}
}
Answer:
In this example, it will set with Fibonacci sequence and includes while loop in the given input.
It should undergo to say in the nth term and sum of (n-1)th and (n-2)th term.
Fibonacci factors are then calculated and bring it to take input from the user n terms.
This is associated with readline and number of terms includes more than 2.