-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFibonacciSeries.java
More file actions
35 lines (32 loc) · 1006 Bytes
/
FibonacciSeries.java
File metadata and controls
35 lines (32 loc) · 1006 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
32
33
34
35
/**
* Calculate Fibonacci Series in Java.
* Fibonacci series is series of natural number where next number is equivalent to the sum of previous two number.
* fn = fn-1 + fn-2.
* The first two numbers of Fibonacci series is always 1, 1.
* First 10 numbers in fibonacci series are:
*
* 1 1 2 3 5 8 13 21 34 55
*/
package com.practice.java;
import java.util.Scanner;
public class FibonacciSeries {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter number : ");
int count = s.nextInt();
s.close();
fibonacci(count);
}
public static void fibonacci(int count) {
int prevNo = 0;
int nextNo = 1;
int fib = 0;
System.out.println("First " + count + " fibonacci numbers are :");
for(int i=1; i <= count;i++){
fib = prevNo + nextNo;
prevNo = nextNo;
nextNo = fib;
System.out.print(prevNo + " ");
}
}
}