Go to Top

Wednesday 3 August 2011

Fibonacci Series : To print and find the sum of the fibonacci series

Question 33 : Write a program in Java to print and find the sum of the fibonacci series upto n terms (value of n to be given by the user) :

Fibonacci series : 


S = 0+1+1+2+3+5+8+......+n terms

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);
    }
}

1 comment:

  1. This is a common question given by most computer teachers at the initial stages.

    ReplyDelete

ShareThis