Program For Fibonacci Series

Posted by Somesh Shinde On Tuesday, 28 June 2016 0 comments

Fibonacci Series ( 1 1 2 3 5 8 13...)



Logic: Sum of previous two numbers will give us next number.

prevnextsum
shifted to prevshifted to next
112
123
235
358
5813
813...
13......
prev will give you fibonacci series

ProgramOutput
class Fibonacci
{
 public static void main(String args[])
 {  
  int prev, next, sum, n;
  prev=next=1
  for(n=1;n<=10;n++)
  {
   System.out.println(prev);
   sum=prev+next;
   prev=next;
   next=sum;
  }
 }
}
      
1
1
2
3
5
8
13
...      

0 comments:

Post a Comment