Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class QuickFibonacciRecursiveSeries {
- /**
- * Program: QuickFibonacciRecursiveSeries.java
- * Purpose: Calculate the first n terms in the Fibonacci series
- * Creator: Chris Clarke
- * Created: 06/10/2013
- */
- private static final int MAX = 92;
- public static long fib(int n) {
- return fiboHelp(0, 1, n);
- } // fibRecursive()
- public static long fiboHelp(long x, long y, int n) {
- // recursive function
- if (n == 0)
- return x;
- else if (n == 1)
- return y;
- else
- return fiboHelp(y, x+y, n-1);
- } // fiboHelp()
- public static void main(String[] args) {
- Scanner keybd = new Scanner(System.in);
- int n = 0;
- long f = 0;
- System.out.print("Enter number between 1 and "+MAX+": ");
- do {
- n = keybd.nextInt();
- if (n<1 || n>MAX) {
- System.out.print("Number outside range [1.."+MAX+"]. Try again: ");
- } // if
- } while (n<1 || n>MAX);
- System.out.println("\nThe first "+n+" numbers in the Fibonacci series are:");
- // print first n Fibonacci numbers
- for (int i=1; i<=n; i++) {
- f = fib(i);
- if (f<0) break;
- System.out.println(i+": " + f);
- } // end for
- } // main()
- } // class QuickFibonacciRecursiveSeries
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement