Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 17-11-2009
Member
 
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???
Reply With Quote
  #2  
Old 17-11-2009
Member
 
Join Date: May 2008
Posts: 2,008
Re: java coding for fibonacci series

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

Quote:
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 ...
Reply With Quote
  #3  
Old 17-11-2009
Member
 
Join Date: Apr 2008
Posts: 2,001
Re: java coding for fibonacci series

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


Quote:
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
Reply With Quote
  #4  
Old 17-11-2009
Member
 
Join Date: May 2008
Posts: 2,293
Re: java coding for fibonacci series

Have you used below logic???...

//Below program require user input
Quote:
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.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


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


All times are GMT +5.5. The time now is 07:42 AM.