Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-22-2008, 09:20 PM
Java Tip's Avatar
Moderator
 
Join Date: Nov 2007
Posts: 1,691
Rep Power: 5
Java Tip will become famous soon enoughJava Tip will become famous soon enough
Default Computing Fibonacci numbers recursively
This program computes Fibonacci numbers using a recursive method.

Code:
 
 public class Fibonacci
{  
    public static void main(String[] args)
    {  
       String input = JOptionPane.showInputDialog("Enter n: ");
       int n = Integer.parseInt(input);
 
       for (int i = 1; i <= n; i++)
       {
          int f = fib(i);
          System.out.println("fib(" + i + ") = " + f);
       }
       System.exit(0);
       
    }
 

    public static int fib(int n)
    {  
       if (n <= 2)
            return 1;
       else 
            return fib(n - 1) + fib(n - 2);
    }
 }
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Computing prime numbers in Java Java Tip java.lang 0 04-12-2008 09:39 PM
Printing Fibonacci Numbers Java Tip java.lang 0 04-09-2008 07:43 PM
recursively searching through arraylists newtojava7 New To Java 1 03-17-2008 03:36 AM
How do you recursively traverse through file folders ? nimraj New To Java 2 11-27-2007 02:45 PM
Fibonacci Algorithm susan New To Java 1 08-07-2007 05:25 AM


All times are GMT +2. The time now is 04:40 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org