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