What's Here?
- Members: 300,293
- Replies: 825,468
- Topics: 137,351
- Snippets: 4,417
- Tutorials: 1,147
- Total Online: 2,131
- Members: 117
- Guests: 2,014
|
Fibonacci sequence is a sequence of numbers defined by
f1 = 1
f2 = 1
fn = fn-1 + fn-2
First ten terms
1, 1, 2, 3, 5, 8, 13, 21, 34, 55
|
Submitted By: mukesh_ranjan18
|
|
Rating:

|
|
Views: 41,571 |
Language: Java
|
|
Last Modified: August 1, 2006 |
Snippet
package com.gpt;
import javax.swing.JOptionPane;
/*
This program computes Fibonacci numbers using a recursive
method.
*/
public class Fibonacci
{
public static void main (String[] args )
{
for (int i = 1; i <= n; i++)
{
int f = fib(i);
System. out. println("fib(" + i + ") = " + f );
}
}
/**
Computes a Fibonacci number.
@param n an integer
@return the nth Fibonacci number
*/
public static int fib(int n)
{
if (n <= 2)
return 1;
else
return fib(n - 1) + fib(n - 2);
}
}
Copy & Paste
|
|
|
|