diff --git a/Fibonacci.java b/Fibonacci.java index 73d7598..59f2ff9 100644 --- a/Fibonacci.java +++ b/Fibonacci.java @@ -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)); } -} +} \ No newline at end of file