Answered # Program to print Fibonacci series till 18

tesuji 135 tesuji 135 Featured Reply NP-complete 42 tesuji 135 NP-complete 42 Need some help with this Array. I am trying to get the sum of the even numbers and the sum of the odd numbers using a for each loop. I know the answers to what I am trying to achive are sum of even = 84 and the sum of ...

0

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

*Edited 6 Years Ago by tesuji*: n/a

0

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

-- tesu

*Edited 6 Years Ago by tesuji*: n/a

1

@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).

0

@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

*Edited 6 Years Ago by tesuji*: n/a

0

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.

This question has already been answered. Start a new discussion instead.

Recommended Articles

When I execute this progammatically, I get a table with row heights much larger than when I do this manually.

Note : Sel is the Word.Selection object and the Clipboard contains an Excel Table.

```
public void AddClipboard()
{
Sel.PasteExcelTable(false,false, false);
var t = Sel.Tables[Sel.Tables.Count];
t.AutoFitBehavior(Word.WdAutoFitBehavior.wdAutoFitContent);
}
```

the function that I created to find the ...