Program to print fibonacci series using function in java.

import java.util.*;
public class function{
    public static void printFibonacciSeries(int n){
        int a=0,b=1,c;
        System.out.print(a+" ");
        System.out.print(b+" ");
        for(int i=2; i<n; i++){
            c=a+b;
            System.out.print(c+" ");
            a=b;
            b=c;
        }
    }
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.println("enter the terms of fibonacci series=");
        int n=sc.nextInt();
        printFibonacciSeries(n);
    }
}

■》Output->

enter the terms of fibonacci series= 8

0 1 1 2 3 5 8 13

Comments

Popular posts from this blog

Introduction of java Programming language.

Stack data structure.