forked from hrsvrdhn/DP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFibonacci.java
More file actions
41 lines (37 loc) · 1014 Bytes
/
Copy pathFibonacci.java
File metadata and controls
41 lines (37 loc) · 1014 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
36
37
38
39
40
41
//Java program for Fibonacci Series using Space
import java.util.*;
class fibonacci
{
public static int[] fib(int n)
{
/* Declare an array to store Fibonacci numbers. */
int f[] = new int[n]; //array of size n
int i,j;
int sum=0;
/* 0th and 1st number of the series are 0 and 1*/
f[0] = 0;
f[1] = 1;
for (i = 2; i < n; i++)
{
/* Add the previous 2 numbers in the series
and store it */
f[i] = f[i-1] + f[i-2];
}
for (j = 0; j < n; j++)
{
/* Add the previous 2 numbers in the series
and store it */
sum+=f[j];
}
System.out.println("Sum: "+sum); //printing the sum
return (f); //returning the complete array
}
public static void main (String args[])
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int a[]=new int[n];
a=fib(n);
System.out.print("Elements:\t"+Arrays.toString(a));
}
}