What's Here?
- Members: 148,802
- Replies: 504,913
- Topics: 79,519
- Snippets: 2,661
- Tutorials: 705
- Total Online: 2,171
- Members: 77
- Guests: 2,094
|
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: 21,610 |
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
|
|
|
Be Social
Reference Sheets
Bye Bye Ads
Monthly Drawing
Top Contributors
Top 10 Kudos This Month
|