Title: Fibonocci
Course: CPS 161: An Intro to Programming with Java - DL MM MASTER

Fibonocci.java


import java.util.Scanner;


public class Fibonocci {

    static int fibFunc(int n)
    {
        int oldest = 0;
        int previous = 1;
        int fib = n;
        if (n >= 2)
        {
            for (int i=2; i <= n; i++)
            {
                fib = oldest+previous;
                oldest = previous;
                previous = fib;
            }
        }
        return fib;
    }



    public static void main(String[] args) {
        int n;
        String continueResponse="y";
        Scanner keyboard = new Scanner(System.in);

        while (continueResponse.equals("y"))
        {
            System.out.println("Enter in N: ");
            n = keyboard.nextInt();

            int oldest = 0;
            int previous = 1;
            int fib = n;
            if (n >= 2)
            {
                for (int i=2; i <= n; i++)
                {
                    fib = oldest+previous;
                    oldest = previous;
                    previous = fib;
                }
            }
            System.out.println("Fibonocci(" + n + ")=" + fib );

            fib = fibFunc(n);
            System.out.println("Fibonocci(" + n + ")=" + fib );

            System.out.println( "Do you want to continue?: (y/n)");
            continueResponse = keyboard.next();
        }
        System.out.println("Have a good day");
        keyboard.close();

    }

}


Last updated: September 19, 2015 11:46 AM