Skip to content

Commit 37022ee

Browse files
Merge pull request #105 from MohammedHassan07/fibonacci
Fibonacci series using recursion (solution)
2 parents 5b92cbb + 839f564 commit 37022ee

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

Recursion/FibonacciSeries.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Program to display the Fibonacci series from 0 to limit provided by the user in input using reucrssion
2+
3+
import java.util.Scanner;
4+
5+
public class FibonacciSeries {
6+
7+
static int number1 = 0; // Declaring and initializing the variables
8+
static int number2 = 1, number3 = 0;
9+
10+
// Main method
11+
public static void main(String[] args) { // Question: Display the fibonacci numbers from 1 to n, where n is
12+
// limit of the fibonacci series, provided by the user as input
13+
// Using recursion.
14+
System.out.println("Enter the limit of fibonacci number"); // Telling the user to enter the limit.
15+
16+
Scanner input = new Scanner(System.in);
17+
int n = input.nextInt(); // Taking the input from user
18+
19+
System.out.print(0 + " "); // Printing the first number
20+
System.out.print(1 + ""); // printing second number
21+
fibonacciNumber(n); // Function call to print the fibonacci series
22+
}
23+
24+
// Method to print fibonacci series (Recursion)
25+
public static void fibonacciNumber(int n) {
26+
27+
if(n > 2) {
28+
29+
number3 = number1 + number2;
30+
number1 = number2;
31+
number2 = number3;
32+
33+
System.out.print(" " + number3); // Printing the number of fibonacci series one by one.
34+
35+
fibonacciNumber(n - 1); // Function call (Recursive call)
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)