Worksheet 2
Worksheet 2
2
The Fibonacci sequence is defined by the following rule. The first 2 values in the sequence are 1, 1. Every subsequent value is the
sum of the 2 values preceding it. Write a Java program that uses both recursive and non-recursive functions to print the nth value
of the Fibonacci sequence.
Write a program to Print nth value of Fibonacci Series using recursive and non-recursive
function.
2. Task to be done:
Write a program to Print nth value of Fibonacci Series using recursive and non-recursive
function.
3. Algorithm/Flowchart:
import java.util.Scanner;
class Main{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int n1=0 ,n2=1 ,n3 ,i ;
System.out.print("Please enter number of output you need : ");
int count = input.nextInt();
System.out.print(n1+" "+n2);
for(i=0;i<count-2;++i)
{
n3=n1+n2;
System.out.print(" "+n3);
n1=n2;
n2=n3;
}}
6. Result/Output:
Learning outcomes (What I have learnt):
Evaluation Grid (To be created as per the SOP and Assessment guidelines by the faculty):