Program which generates the N terms of Fibonacci series using function.

import java.io.*;
class number
{ int a=0,b=1,x,sum=0;
void fibonacci(int x)
{
for(int i=0;i<x;i++)
{
sum=a+b;
a=b;
b=sum;
System.out.print(sum+", ");
}
}
}
class main
{ public static void main(String[] asd)throws IOException
{
BufferedReader obj= new BufferedReader(new InputStreamReader(System.in));
int n;
String num;
number a=new number();
System.out.println("Enter a number::");
num=obj.readLine();
n=Integer.parseInt(num);
System.out.print("0, ");
System.out.print("1, ");
a.fibonacci(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