Skip to content

Commit 4af3c3e

Browse files
committed
Print first n Fibonacci Numbers
1 parent c4011a4 commit 4af3c3e

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.gfg.problem;
2+
3+
4+
import java.util.*;
5+
6+
public class FibonacciProblem {
7+
8+
public static void main(String[] args) {
9+
//taking input using Scanner class
10+
Scanner sc = new Scanner(System.in);
11+
12+
//taking total count of testcases
13+
int t = sc.nextInt();
14+
15+
while (t-- > 0) {
16+
//taking total number of elements
17+
int n = sc.nextInt();
18+
19+
//calling printFibb() method
20+
long[] res = printFibb(n);
21+
22+
//printing the elements of the array
23+
for (int i = 0; i < res.length; i++)
24+
System.out.print(res[i] + " ");
25+
System.out.println();
26+
}
27+
}
28+
29+
30+
// } Driver Code Ends
31+
32+
33+
//User function Template for Java
34+
35+
36+
public static long[] printFibb(int n) {
37+
long[] ans = new long[n];
38+
ans[0] = 1;
39+
if (n == 1) {
40+
return ans;
41+
}
42+
ans[1] = 1;
43+
for (int i = 2; i < n; i++) {
44+
ans[i] = ans[i - 1] + ans[i - 2];
45+
}
46+
return ans;
47+
48+
}
49+
50+
}

0 commit comments

Comments
 (0)