Go to Top

Saturday 1 October 2011

To print and find the sum of the fibonacci series

Question 47 : Fibonacci series = 0,1,1,2,3,5,8,13.......,n

Print and find the sum of the series.

Java Program :

public class fibonacci_series
{
 public static void main(int n)
 {
     int a=0;
     int b=1;
     int s=0;
     int c=0;
     int i;
     s=s+a+b;
     System.out.print("The fibonacci series = "+a+","+b+",");
     for(i=3;i<=n;i++)
     {
         c=a+b;
         System.out.print(c+",");        
         a=b;
         b=c;
         s=s+c;
        }
        System.out.println("upto "+n+" terms completed");
        System.out.println("\nThe sum of the fibonacci series upto "+n+" terms = "+s);
    }
}
       

21 comments:

  1. This helped me a lot in the nick of time..
    Thnx a lot...

    ReplyDelete
  2. god, this helped more than my computer teacher ever did.
    thnx a lot!

    ReplyDelete

ShareThis