Advertisement
brilliant_moves

FibSeries.java

Nov 4th, 2015
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.37 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class FibSeries {
  4.  
  5.         /**     Program:        FibSeries.java
  6.         *       Purpose:        Display the first n terms in the Fibonacci Series
  7.         *       Creator:        Chris Clarke
  8.         *       Created:        04.11.2015
  9.         */
  10.  
  11.         public static void main(String[] args) {
  12.                 Scanner scan = new Scanner(System.in);
  13.                 int n;
  14.  
  15.                 System.out.println("I will compute the first n terms in the Fibonacci Series.");
  16.  
  17.                 do {
  18.                         System.out.print("Please enter a value for n: ");
  19.                         n = scan.nextInt();
  20.                         if (n>40) System.out.println("Number too big! Must be 40 or less");
  21.                         if (n<1) System.out.println("Number too small! Must be 1 or more");
  22.                 } while (n>40 || n<1);
  23.  
  24.                 showFibonacci(n);
  25.         }
  26.  
  27.         public static void showFibonacci(int n) {
  28.                 for (int i=1; i<=n; i++) {
  29.                         switch(i) {
  30.                                 case 1 :
  31.                                 case 2 :        F[i] = 1; break;
  32.                                 default  :      F[i] = F[i-2] + F[i-1];
  33.                         }
  34.             System.out.println(F[i]);
  35.                 }
  36.         }
  37.  
  38.         private static int[] F = new int[41];
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement