OOPM: java program for fibonacci series

//fibonacci w/o recursion
import java.io.*;

class Fibbo{
public static void main(String args[])
{
int n,x,y,temp;
n = Integer.parseInt(args[0]);
x=0;
y=1;
do{
System.out.print(x + ” “);
temp = x;
x = y;
y = temp + y;
n–;
}while(n>0);
}
}

*********************************************************************

//fibonacci with recursion
import java.io.*;

class Fibbo
{
void fib(int x, int y, int n)
{
int temp;
if(n<=0)
System.out.print(x + ” “);
else
{
System.out.print(x + ” “);
temp = x;
x = y;
y+=temp;
fib(x,y,–n);
}
}
}

class FibboRec{
public static void main(String args[])
{
int n;
n = Integer.parseInt(args[0]);
Fibbo f = new Fibbo();
System.out.print(“Fibonacci series:”);
f.fib(0,1,n);
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s