Java Program to find fibonacci series of a number using methods

Program

import java.util.Scanner;
public class FibonacciFunction {
    void generateFibonacci(int range) {
        if (range <= 0) {
            System.out.println("Cannot generate fibonacci series");
            System.exit(0);
        }
        int first = 0;
        int second = 1;
        System.out.println("Fibonacci Series:");
        if (range == 1)
            System.out.println(first);
        else if (range & gt; = 2) {
            System.out.print(first + "\t" + second);
            for (int i = 0; i & lt; range - 2; i++) {
                int fib = first + second;
                System.out.print("\t" + fib);
                first = second;
                second = fib;
            }
            System.out.println();
        }
    }
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the range of fibonacci series");
        int range = sc.nextInt();
        FibonacciFunction fibonacci = new FibonacciFunction();
        fibonacci.generateFibonacci(range);
    }
}

Here's a detailed explanation of your FibonacciFunction program.

Program Description

The program generates the Fibonacci series up to a specified range provided by the user. It uses a method (generateFibonacci) inside a class (FibonacciFunction) to encapsulate the logic for Fibonacci sequence generation.

Code Walkthrough

1. Importing the Scanner Class

import java.util.Scanner;
  • The program uses the Scanner class to take input from the user.

2. Class Definition

public class FibonacciFunction
  • The program defines a class named FibonacciFunction.

3. Method to Generate Fibonacci Series

void generateFibonacci(int range)
  • Purpose: This method contains the core logic to generate the Fibonacci series.
  • Parameters: Takes one parameter, range, which specifies the number of terms in the Fibonacci series to generate.

4. Input Validation

if (range <= 0) {
    System.out.println("Cannot generate fibonacci series");
    System.exit(0);
}
  • Checks if the user input is invalid (non-positive number).
  • If invalid, prints an error message and exits the program.

5. Initialize First Two Fibonacci Numbers

int first = 0;
int second = 1;
  • first: The first Fibonacci number.
  • second: The second Fibonacci number.

6. Print the Series

System.out.println("Fibonacci Series:");
  • Prints a header for the Fibonacci series.

7. Handle Special Cases

  • Case: range == 1

    • Prints only the first Fibonacci number (0).
    if (range == 1)
        System.out.println(first);
    
  • Case: range >= 2

    • Prints the first two numbers (0 and 1).
    • Calculates the remaining numbers in the series.
    else if (range >= 2) {
        System.out.print(first + "\t" + second);
    

8. Generate Remaining Terms

for (int i = 0; i < range - 2; i++) {
    int fib = first + second;
    System.out.print("\t" + fib);
    first = second;
    second = fib;
}
  • Uses a for loop to calculate and print the remaining terms in the Fibonacci series:
    1. Calculates the next term (fib = first + second).
    2. Prints the term.
    3. Updates first and second for the next iteration.

9. Main Method

public static void main(String args[]) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter the range of fibonacci series");
    int range = sc.nextInt();
    FibonacciFunction fibonacci = new FibonacciFunction();
    fibonacci.generateFibonacci(range);
}
  • Step 1: Creates a Scanner object to take input.
  • Step 2: Prompts the user to enter the range.
  • Step 3: Reads the input value into range.
  • Step 4: Creates an object of the FibonacciFunction class.
  • Step 5: Calls the generateFibonacci() method to generate and print the series.

Execution Steps

  1. User enters range = 8.
  2. generateFibonacci(8) is called.
  3. Outputs:
    • First two terms: 0, 1.
    • Loop calculates and prints:
      • Iteration 1: fib = 0 + 1 = 1, prints 1.
    • Iteration 2: fib = 1 + 1 = 2, prints 2.
    • Iteration 3: fib = 1 + 2 = 3, prints 3.
    • Iteration 4: fib = 2 + 3 = 5, prints 5.
    • Iteration 5: fib = 3 + 5 = 8, prints 8.
    • Iteration 6: fib = 5 + 8 = 13, prints 13.

Output

Enter the range of fibonacci series
8
Fibonacci Series:
0	1	1	2	3	5	8	13