Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class FibSeries {
- /** Program: FibSeries.java
- * Purpose: Display the first n terms in the Fibonacci Series
- * Creator: Chris Clarke
- * Created: 04.11.2015
- */
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- int n;
- System.out.println("I will compute the first n terms in the Fibonacci Series.");
- do {
- System.out.print("Please enter a value for n: ");
- n = scan.nextInt();
- if (n>40) System.out.println("Number too big! Must be 40 or less");
- if (n<1) System.out.println("Number too small! Must be 1 or more");
- } while (n>40 || n<1);
- showFibonacci(n);
- }
- public static void showFibonacci(int n) {
- for (int i=1; i<=n; i++) {
- switch(i) {
- case 1 :
- case 2 : F[i] = 1; break;
- default : F[i] = F[i-2] + F[i-1];
- }
- System.out.println(F[i]);
- }
- }
- private static int[] F = new int[41];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement