Printing nth Fibonacci element
(Non-Recursion)

Aim:
To write a java program that uses non-recursive procedure to print the nth value in the Fibonacci sequence.

Description: 
The Fibonacci sequence (denoted by f0, f1, f2 …) is as follows: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 … That is, f0 = 1 and f1 = 1 and each succeeding term is the sum of the two preceding terms. Typically, recursion is more elegant and requires fewer variables to make the same calculations but stacking of arguments, while invisible to the user, is still costly in time and space. We can implement this by using iterative procedure also without Recursion.

Program:
class Demo
{
int fib(int n)
{
int a=1, b=1, k=3, c=0;
if( n== 1)
return(1);
else if( n==2 )
return(1);
else
{
while(k <= n)
{
c = a + b;
a = b;
b = c;
k++;
}
return (c);
}
}
}
class Fib2
{
public static void main(String args[])
{ Demo ob=new Demo();
System.out.println("The 10th fibonacci element is " + ob.fib(10));
}
}