Peoblem:3.02


Question 3.02: Write a program called Fibonacci to print the first 20 Fibonacci numbers F(n), where F(n)=F(n–1)+F(n–2) and F(1)=F(2)=1. Also compute their harmonic mean. The output shall look like:

The first 20 Fibonacci numbers are:

1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765

The average is **


Solution: 

package com.company;
import java.util.Scanner;
public class Lab03Problem2 {
    public static void main(String[] args) {
        Scanner Obj = new Scanner(System.in);
        System.out.print("Enter the size: ");
        int n = Obj.nextInt();
        int r = fid(n);
        System.out.println(r);
    }
    static int  fid(int x){
        if(x<=1)
            return x;
            return fid(x-1) + fid(x-2);


    }
}

Previous Post Next Post