Monday, 6 July 2015

Write Program for Fibonacci Series ?

package com.JavaInterview.Programs;

import java.util.*;

/**
 * Find Out Fibonacci Series
 * 
 * @author:http://javainterviewprograms.blogspot.in/
 * 
 */

public class FibonacciSeries {

 public static void main(String args[]) {
  Scanner input = new Scanner(System.in);

  System.out.println("Please enter length of series : ");
  int length = input.nextInt();
  int feb[] = new int[length];
  feb[0] = 0;
  feb[1] = 1;
  for (int i = 2; i < length; i++) {
   feb[i] = feb[i - 1] + feb[i - 2];
  }
  System.out.println("Fibonacci Series of length " + length + ":");
  for (int i = 0; i < length; i++) {
   System.out.print(feb[i] + " ");
  }
 }

}

Output :
Please enter length of series : 
10
Fibonacci Series of length 10:
0 1 1 2 3 5 8 13 21 34 


No comments:

Post a Comment

Difference between final, finally and finalize()?

final :Final is a keyword. Final is used to apply restrictions on class, method and variable. Final class can't be inherited, final ...