Class 11

Generate Fibonacci Series in Java

Write a program to display the first N Fibonacci numbers, and also find and display their sum. The value of N has to be input by the user.


class Fibonacci{
    public static void main(String args[])
    throws IOException{
        InputStreamReader in = new InputStreamReader(;
        BufferedReader br = new BufferedReader(in);
        System.out.print("N = ");
        int n = Integer.parseInt(br.readLine());
        long sum = 0;
        int a = 0;
        int b = 1;
        System.out.print("Fibonacci Series: ");
        for(int i = 1; i <= n; i++){
            long t = fibo(i);
            sum += t;
            System.out.print(t + "\t");
        System.out.println("\nSeries sum = " + sum);
    public static long fibo(int n){
        if(n == 1)
            return 0L;
        else if(n == 2)
            return 1L;
            return fibo(n - 1) + fibo(n - 2);

By admin

I am a Computer Science teacher in one of the renowned schools in India. I teach Java programming language to my students, and I maintain a website

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.