Fibonacci Series – using Java (Source Code)

I made this Fibonacci Series source code when I was in college. 🙂

The Fibonacci sequence is a series of numbers where a number is the addition of the last two numbers, starting with 0, and 1.

This simple program will ask the user to input a number. That number will be used as a limit for the Fibonacci series of numbers 🙂

/*  From Techie.Click
    By JP  */
import java.util.Scanner;
public class Fibonacci {

    static Scanner input = new Scanner(System.in);
    public static void main(String[] args) {
        try {      
            System.out.println("This program will generate a fibonacci series based on the inputted number.");
            System.out.print("Enter an integer: ");
            int limit = input.nextInt();
            int pred=0, pres=0, nxt;
            for (nxt = 1; nxt <= limit; nxt = pred + pres){
                if(pres==0)
                    System.out.print("The Fibonacci series is: " + pred + "," + nxt);
                else {
                    System.out.print("," + nxt); 
                }
                pred = pres;
                pres = nxt;
            }
            System.out.println("\nEnd of program.");
        }
        catch(Exception e) {
            System.out.println("An error has occured. " + e);
        }
    }
}

Fibonacci Series – using Java (Source Code)

Be the first to comment

Leave a Reply

Your email address will not be published.


*


This site uses Akismet to reduce spam. Learn how your comment data is processed.