Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner; // for user input
- public class Fibonacci {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- int n;
- System.out.println("I will compute the Nth term 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);
- System.out.println((int) fib(n));
- }
- public static double fib(int n) {
- for (int i=1; i<=n; i++) {
- switch(i) {
- case 1 : F[i] = 1; break;
- case 2 : F[i] = 1; break;
- default : F[i] = F[i-2] + F[i-1];
- }
- }
- return F[n];
- }
- private static double[] F = new double[101];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement