Saturday, 2 July 2016

Write a Java Program to Print Fibonacci Series upto a given Number

import java.util.*;
public class fibonacci {

    public static void main(String[] args) {
           
        Scanner k=new Scanner(System.in);
        System.out.print("Enter range: ");
        int n=k.nextInt();
        int a=0;
        int b=1;
        System.out.print(a+" ");
        System.out.print(b+" ");
     
        for(int i=3;i<=n;i++)
        {
            int c=a+b;
            System.out.print(c+" ");
            a=b;
            b=c;
                 
        }
    }
 
}

OUTPUT:

Enter range: 10
0 1 1 2 3 5 8 13 21 34



No comments:

Post a Comment

Popular Posts