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.
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.
1. Importing the Scanner Class
import java.util.Scanner;
- The program uses the
Scannerclass 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); - Prints only the first Fibonacci number (
-
Case:
range >= 2- Prints the first two numbers (
0and1). - Calculates the remaining numbers in the series.
else if (range >= 2) { System.out.print(first + "\t" + second); - Prints the first two numbers (
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
forloop to calculate and print the remaining terms in the Fibonacci series:- Calculates the next term (
fib = first + second). - Prints the term.
- Updates
firstandsecondfor the next iteration.
- Calculates the next term (
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
Scannerobject 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
FibonacciFunctionclass. - Step 5: Calls the
generateFibonacci()method to generate and print the series.
Execution Steps
- User enters
range = 8. generateFibonacci(8)is called.- Outputs:
- First two terms:
0,1. - Loop calculates and prints:
- Iteration 1:
fib = 0 + 1 = 1, prints1.
- Iteration 1:
- Iteration 2:
fib = 1 + 1 = 2, prints2. - Iteration 3:
fib = 1 + 2 = 3, prints3. - Iteration 4:
fib = 2 + 3 = 5, prints5. - Iteration 5:
fib = 3 + 5 = 8, prints8. - Iteration 6:
fib = 5 + 8 = 13, prints13.
- First two terms:
Output
Enter the range of fibonacci series
8
Fibonacci Series:
0 1 1 2 3 5 8 13