Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions Fibonacci.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,40 @@
import java.util.*;
class fibonacci
{
static int fib(int n)
public static int[] fib(int n)
{
/* Declare an array to store Fibonacci numbers. */
int f[] = new int[n+2]; // 1 extra to handle case, n = 0
int i;
/* 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++)
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];
}

return f[n];

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();
System.out.println(fib(n));
int a[]=new int[n];
a=fib(n);
System.out.print("Elements:\t"+Arrays.toString(a));
}
}
}