Wednesday, March 9, 2011

Fibonacci series in Visual C++

# include <stdio.h>
int fnFibonacci(int);
int main()
{
int nNumber;
printf("Enter the number of items limit\n");
fflush(stdin);
scanf("%d",&nNumber);

fnFibonacci(nNumber); // call function

return 0;// for successful execution
}

int fnFibonacci(int nNo) // called function
{
int a=0;
int b=1;
int nLoop,c; // Third variable
if (nNo <= 2)
{
printf("%d\t%d",a,b);
}
else
{
printf("%d\t%d",a,b);
for(nLoop = 3; nLoop<= nNo;nLoop++)
{
c = a + b;
a = b;
b = c;
printf("\t%d",c);
}
}
return 0;// for successful execution
}

OUTPUT

Enter the number of items limit
10
0 1 1 2 3 5 8 13 21 34Press

No comments:

Post a Comment