Please let me know where i have done the mistake

#include <stdio.h>
#include <conio.h>
#include <math.h>

void main()


{
int f=0,s=1,n;

n=f+s;
if(n<=18);

{
printf("%d" ,n);

f=s;
s=n;
n=f+s;

}




getch();

}

hi,

you should remove the ; in this line: if(n<=18);

-- tesu

Still no difference :(

all it prints is " 1 "

Still no difference :(

all it prints is " 1 "

that s true, you should also replace if by for loop:

//if(n<=18)

for (int i=1; i <=18; i++)
{
printf("%2d    %5d\n", i ,n);
n=f+s;
f=s;
s=n;
n=f+s;
}
/*
now result is:

 1        1
 2        2
 3        3
 4        5
 5        8
 6       13
 7       21
 8       34
 9       55
10       89
11      144
12      233
13      377
14      610
15      987
16     1597
17     2584
18     4181

>>> Your algorithm isn't completely correct for fibo(18) = 2584. Also f1=f2=1 and f0=0.
Possibly s contains correct series.
*/

-- tesu

he, there seems to be a mistake in your algorithm, because fib(18) is 2584.

-- tesu

@tesu:
The algorithm is correct. Its just that the first tw0 terms of the series must be printed separately. fibo(18) is 2584 iff fibo(0) = 0. So what you are printing as your first term should be the 2nd term (ie fibo(2) if we take the first term as fibo(0)).

generally the series is :

fibo(0)------ 0
fibo(1)------ 1
fibo(2)------ 1
fibo(3)------ 2
fibo(4)------ 3

...............
...............

fibo(18)------ 2584


hope it helps.


P.S: @xaop
once you get the logic correct spend some time on coming up with meaningful variable names. Also use proper indentation. As of now, a small advice, never use void main(). Its plain wrong (and disrespect to the creators of C).

@tesu:
The algorithm is correct. Its just that the first tw0 terms of the series must be printed separately. fibo(18) is 2584 iff fibo(0) = 0. So what you are printing as your first term should be the 2nd term (ie fibo(2) if we take the first term as fibo(0)).

generally the series is :

fibo(0)------ 0
fibo(1)------ 1
fibo(2)------ 1
fibo(3)------ 2
fibo(4)------ 3

...............
...............

fibo(18)------ 2584


hope it helps.


P.S: @xaop
once you get the logic correct spend some time on coming up with meaningful variable names. Also use proper indentation. As of now, a small advice, never use void main(). Its plain wrong (and disrespect to the creators of C).

Sorry, I don't agree, NPC.

His algorithm does not produce FIB(18). So it must be considered to be incorrect, even if we don't regard his mistake in if-statement.

Here is a somewhat more compact iterative Fibonacci:

for(int i=0,n=18,x,y=0,z=1;i<=n;i++,x=y,y=z,z=x+z,printf("%3d %5d\n",i-1,x));

-- tesu

Nope, i think you didn't go through my post completely. I said his algo is correct provided you print the first two ie fibo(0) and fibo(1) separately. Also it seems you have introduced an error (a typo, maybe). Look at line no. 6 and 9 of the code which you modified previously. the OP had n=f+s once inside but you made it twice.

Well, your compact code is definitely right but is somewhat hard to read and understand.

I was talking of something like this::

#include <stdio.h>

int main(){
    int f=0,s=1,n;
    n=f+s;
    printf("%2d    %5d\n", 0, f);  //fibo(0), separately printed
    printf("%2d    %5d\n", 1, s);  //fibo(1), separately printed

    for (int i=2; i <=18; i++){
	printf("%2d    %5d\n", i ,n);
	f=s;
	s=n;	
	n=f+s;
    }
/*
now result is:

 0        0
 1        1
 2        1
 3        2
 4        3
 5        5
 6        8
 7       13
 8       21
 9       34
10       55
11       89
12      144
13      233
14      377
15      610
16      987
17      1597
18      2584

though the OP wanted upto 18 terms and that should have printed upto fibo(17) but i kept fibo(18) coz that seems to be the centre of all confusion :)
*/

}

hope it clarifies.

Thank you :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.