-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFibonacci.java
More file actions
30 lines (24 loc) · 850 Bytes
/
Copy pathFibonacci.java
File metadata and controls
30 lines (24 loc) · 850 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
30
import java.util.Scanner;
public class Fibonacci {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("welcome to fibonacci series");
System.out.println("please enter the number upto which the series has to be printed");
int num = input.nextInt();
System.out.println("here is the fibonacci series");
printFibonacci(num);
}
public static void printFibonacci(int num) {
if (num<0) return;
System.out.println("0 ");
if (num ==0) return;
System.out.println("1 ");
int first = 0, second = 1;
while(first + second <= num) {
int third = first + second;
System.out.print(third + " ");
first = second;
second = third;
}
}
}