forked from khushal87/Data-structures-project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFibonacci.java
More file actions
51 lines (44 loc) · 1.1 KB
/
Fibonacci.java
File metadata and controls
51 lines (44 loc) · 1.1 KB
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
42
43
44
45
46
47
48
49
50
51
/*
Print first N Fibonacci numbers and their average.
N should be command line input.
*/
// Created by akashbhalotia
import java.util.Scanner;
class Fibonacci
{
private int a[],N;
private double avg;
public Fibonacci(int N) //parameterized constructor
{
this.N=N;
a=new int[this.N];
avg=0.0;
}
public void solve() //solves the problem
{
int sum=0,i;
a[0]=a[1]=1;
for(i=2;i<N;i++) //finds the next Fibo number
a[i]=a[i-1]+a[i-2];
for(i=0;i<N;i++) //computes sum of the numbers
sum+=a[i];
avg=(double)(sum)/N;
}
public void display() //displays the answer computed
{
System.out.print("The first "+N+" Fibonacci numbers are: ");
for(int i=0;i<N-1;i++)
System.out.print(a[i]+" ");
System.out.println(a[N-1]);
System.out.println("The average is "+avg);
}
}
class Solver
{
public static void main(String[] args)
{
Fibonacci obj=new Fibonacci(Integer.parseInt(args[0]));
obj.solve();
obj.display();
}
}