Monday, 13 July 2015

Java Programs[Important]

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    program that uses both recursive and non-recursive functions to print the nth value of the Fibonacci sequence.

Program :

/*Non Recursive Solution*/ import   .util.Scanner; class Fib { public static void main(String args[ ]) { Scanner input=new Scanner(System.in); int i,a=1,b=1,c=0,t; System.out.println("Enter value of t:"); t=input.nextInt(); System.out.print(a); System.out.print(" "+b); for(i=0;i<t-2;i++) { c=a+b; a=b;





 
b=c; System.out.print(" "+c); } System.out.println(); System.out.print(t+"th value of the series is: "+c); } }