File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments