forked from sPredictorX1708/Ultimate-Java-Resources
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFibonacci.java
More file actions
31 lines (27 loc) · 961 Bytes
/
Fibonacci.java
File metadata and controls
31 lines (27 loc) · 961 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
31
/**
* Fibonacci series in Java
* Program that prints the first N numbers of the Fibonacci series
* The first number in the series is 1, the second number is 1, and each of the
* following is the sum of the previous two
*
* @author IryaDev
*/
import java.util.*;
public class Fibonacci{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
long number,fib_1 = 1,fib_2 = 1,i;
do{
System.out.print("Enter a number greater than one: ");
number = sc.nextInt();
}while(number<=1);
System.out.println("The first " + number + " terms of the Fibonacci series are:");
System.out.print(fib_1 + " ");
for(i=2;i<=number;i++){
System.out.print(fib_2 + " ");
fib_2 = fib_1 + fib_2;
fib_1 = fib_2 - fib_1;
}
System.out.println();
}
}