Posts Tagged fibonacci series
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);
}
}
Program for Fibonacci series.
Posted by admin in C Examples on July 18th, 2009
A Fibonacci Sequence is defined as follows: the first and second terms in the sequence are 0 and 1. Subsequent terms are found by adding the preceding two terms in the sequence. Write a C program to generate the first n terms of the sequence.
#include <stdio.h>
void main()
{
int num1=0, num2=1,no,counter,fab;
clrscr();
printf("<===========PROGRAM TO FIND THE FIBONACCI SERIES UP TO N NO. IN SERIES=========>");
printf("\n\n\n\t\tENTER LENGTH OF SERIES (N) : ");
scanf("%d",&no);
printf("\n\n\t\t\t<----FIBONACCI SERIES---->");
printf("\n\n\t\t%d %d",num1,num2);
//LOOP WILL RUN FOR 2 TIME LESS IN SERIES AS THESE WAS PRINTED IN ADVANCE
for(counter = 1; counter <= no-2; counter++)
{
fab=num1 + num2;
printf(" %d",fab);
num1=num2;
num2=fab;
}
getch();
}
Program for FIBONACCI SERIES in C++.
Posted by admin in C++ Examples on July 2nd, 2009
#include<iostream.h> //Including header files
int fibnac(int n); //function prototype for fibnac series
void main()
{
int n,i;
char ch;
do
{
//Taking the no. of terms to be found out
cout<<"How many terms of Fibonacci Series do you wanna calculate\n";
cin>>n;
for(i=1;i<=n;i++) //Calculating the terms one-by-one and writing to the VDU
{
int p=fibnac(i);
cout<<p<<" ";
}
cout<<endl;
cout<<"Do you wanna continue?(Y/N)\t";
cin>>ch;
cout<<endl;
}while(ch=='y'||ch=='Y');
}
int fibnac(int p) //Function to calculate a particular fibonacci term
{
if (p==1) return(1);
if (p==2) return(2);
if(p>2) return(fibnac(p-1)+fibnac(p-2));
}


Recent Comments