-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFibonacci.java
More file actions
29 lines (26 loc) · 882 Bytes
/
Fibonacci.java
File metadata and controls
29 lines (26 loc) · 882 Bytes
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
import java.util.Scanner;
public class Fibonacci {
public static void main(String[] args) {
try (Scanner input = new Scanner(System.in)) {
System.out.println("Welcome to Fibonacci Series!");
System.out.print("Enter the number of terms you want in the series: ");
int first = input.nextInt();
System.out.println("Here is the Fiboncci Series.");
printFibonacci(first);
}
}
public static void printFibonacci(int num) {
if (num < 0)
return;
System.out.print("0");
if (num == 0)
return;
int first = 0, second = 1;
while (first + second <= num) {
int third = first + second;
System.out.print(" " + third);
first = second;
second = third;
}
}
}