Results 1 to 4 of 4

Thread: java coding for fibonacci series

  1. #1
    Join Date
    Jan 2009
    Posts
    37

    java coding for fibonacci series

    Hi friends,

    I am BSc(I.T) student. I have to write fibonacci series program using java programming.I have written the code but when I run the program either I get wrong result or unknown compile time error.

    Can anybody provide me the source code for the fibonacci series even the hint also will works for this???

  2. #2
    Join Date
    May 2008
    Posts
    2,012

    Re: java coding for fibonacci series

    I used below code to get Output as a fibonacci series:

    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)
    {
    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);

    }

    /**
    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);
    }
    }
    Please try this coding ...

  3. #3
    Join Date
    Apr 2008
    Posts
    2,005

    Re: java coding for fibonacci series

    It is very easy code.I don't thing that you will get errors for below code:


    public class FibonnaciSeries {

    public void generateSeries(int num) {

    int f1, f2 = 0, f3 = 1;

    System.out.println(”fib(0) = ” + f2);

    for (int i = 1; i <= num; i++) {
    System.out.println("fib(" + i + ") = " + f3);
    f1 = f2;
    f2 = f3;
    f3 = f1 + f2;
    }
    }

    public static void main(String[] args) {
    System.out.println("*****Fibonnaci Series*****");
    FibonnaciSeries fb = new FibonnaciSeries();
    fb.generateSeries(10);
    }

    }


    The Output of above program will be:
    1, 1, 2, 3, 5, 8, 13, 21, 34, 55

  4. #4
    Join Date
    May 2008
    Posts
    2,297

    Re: java coding for fibonacci series

    Have you used below logic???...

    //Below program require user input
    public class fibonacci
    {

    public static void main(String args[])
    {
    int prev1 = 1, prev2 = 1, current = 1;int n;
    n = System.read("enter number: ");
    for (int i = 3; i <= n; i++)
    {
    current += prev2;
    prev2 = prev1;
    prev1 = current;
    System.out.println(" " + current);
    }

    }
    }
    Last edited by Zecho; 17-11-2009 at 10:46 AM.

Similar Threads

  1. Downloading java games for Motorola series
    By Lobjeya in forum Portable Devices
    Replies: 5
    Last Post: 13-04-2011, 10:29 AM
  2. java coding
    By unikshegz in forum Software Development
    Replies: 1
    Last Post: 27-10-2010, 01:56 AM
  3. How to use the pop_heap() in C++ coding
    By Garrick in forum Software Development
    Replies: 4
    Last Post: 05-03-2010, 05:28 PM
  4. Code Conventions for JAVA Coding
    By kex in forum Software Development
    Replies: 2
    Last Post: 06-05-2009, 11:23 PM
  5. Fibonacci in C++
    By NAVINk in forum Software Development
    Replies: 5
    Last Post: 25-10-2008, 03:25 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,285,140.47109 seconds with 17 queries