Wednesday, 9 October 2013

Program to calculate n fibo using constructor

//Program to calculate n fibo using constructor
//Developed by Ahlam Ansari

import java.util.*;

class Fibonacci
{
    int n1,n2,n3,limit;
   
    Fibonacci(int n)
    {   
        limit = n;
        n1=0;
        n2=1;
    }   
   
    void series()
    {
        System.out.println("The Fibonacci Series up to "+ limit +" are:");
        System.out.print(n1+" "+n2+" ");
        int i;
        for(i=3;i<=limit;i++)
        {
            n3=n1+n2;
            System.out.print(n3+" ");
            n1=n2;
            n2=n3;
        }
    }
}

class MainFibo
{
    public static void main(String ahlam[])
    {
        Scanner sc= new Scanner(System.in);
        System.out.println("Enter the Limit");
        int n = sc.nextInt();
        Fibonacci f= new Fibonacci(n);
        f.series();
    }   
}


/*
C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>javac MainFibo.java

C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>java MainFibo
Enter the Limit
10
The Fibonacci Series up to 10 are:
0 1 1 2 3 5 8 13 21 34
C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>
*/

No comments:

Post a Comment