Sunday, July 12, 2009

I want 2 write program to print FIBONACCi series in c? i am using turbo c.?

#include%26lt;stdio.h%26gt;


#include%26lt;conio.h%26gt;


main()


{


clrscr();


int j,a,b,c,g;


float d;


a=1;


b=0;


printf("how many fibonacci num u want????");


scanf("%d",%26amp;j);


for(int n;n%26lt;=j;n++)


{


c=a+b;


d=c%2;


if(d==0)


{


g=a;


a=c;


}


else


{


g=b;


b=c;


}


printf("1 %d",c);


}


getch();


}

I want 2 write program to print FIBONACCi series in c? i am using turbo c.?
Your program probably won't compile and isn't computing the fibonacci numbers. The thing to realize is that each number in the sequence is the sum of the previous two numbers. So the simplest solution is to write it exactly that way:





int fibonacci(unsigned int n) {


if (n==0) {


return 0; /* special case for fib(0)=0 */


} else if (n==1) {


return 1; /* special case for fib(1)=1 */


} else {


return fibonacci(n-1)+fibonacci(n-2); /* otherwise sum of previous two numbers*/


}


}





To compute the n'th number in the sequence, just call fibonacci(n). You should be able to fill in the details...





This is not the most efficient implementation (there are a lot of other faster implementations and ones that require less memory) but it's probably the easiest to understand. It uses a technique called "recursion" - which means you define a function in terms of itself. That just happens to be a natural way to describe the fibonacci sequence. It looks a bit strange a first, but read the code carefully, and imagine what happens when you call the function with different values and you'll soon see what's going on and why it works.

yu gi oh cards

No comments:

Post a Comment