FibonacciNumber

FibonacciNumber - + "of the desired number in " +...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
//Recursion: Fibonacci Number / import java.util.*; i public class FibonacciNumber { static Scanner console = new Scanner(System.in); public static void main(String[] args) { int firstFibNum; int secondFibNum; int nthFibonacci; System.out.print("Enter the first Fibonacci number: "); firstFibNum = console.nextInt(); System.out.println(); System.out.print("Enter the second Fibonacci number: "); secondFibNum = console.nextInt(); System.out.println(); System.out.print("Enter the position "
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: + "of the desired number in " + "the Fibonacci \nsequence: "); nthFibonacci = console.nextInt(); System.out.println(); System.out.println("The " + nthFibonacci + "th Fibonacci number of " + "the sequence is: " + rFibNum(firstFibNum, secondFibNum, nthFibonacci)); } public static int rFibNum(int a, int b, int n) { if (n == 1) return a; else if (n == 2) return b; else return rFibNum(a, b, n - 1) + rFibNum(a, b, n - 2); } }...
View Full Document

This note was uploaded on 06/17/2011 for the course COMP 2301 taught by Professor Staff during the Spring '10 term at Kennesaw.

Ask a homework question - tutors are online