# Finding Golden ratio in fibonacci series

• (2 Pages)
• 1
• 2

## 28 Replies - 3287 Views - Last Post: 23 December 2016 - 01:52 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=400133&amp;s=2c67d2200c5b989a87a2276bde1bbe30&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

Reputation: 0
• Posts: 71
• Joined: 04-November 16

# Finding Golden ratio in fibonacci series

Posted 21 December 2016 - 12:49 PM

Hi , I just started coding and doing lots of exercise and there is a problem which is I couldn't. The problem is : display Fibonacci series and its golden ratio.
I wrote this code but it gives golden ratio as "0"

```#include<stdio.h>
int main()
{
int n,i,a,b,c;
printf("Enter a number: ");
scanf("%d",&n);
i=1;
a=0;
b=1;
while(i<=n)
{
printf("%d ",a);
c = a + b;
a = b;
b = c;
i++;
}
p=a/b;
printf("Golden ratio is %lf", p);
return 0;
}

```

Is This A Good Question/Topic? 0

## Replies To: Finding Golden ratio in fibonacci series

### #2 jimblumberg

Reputation: 5441
• Posts: 16,966
• Joined: 25-December 09

## Re: Finding Golden ratio in fibonacci series

Posted 21 December 2016 - 12:54 PM

Quote

I wrote this code but it gives golden ratio as "0"

And what exactly did you expect the program to output? What did you input into the program?

By the way where did you define the variable p?

Jim

Reputation: 0
• Posts: 71
• Joined: 04-November 16

## Re: Finding Golden ratio in fibonacci series

Posted 21 December 2016 - 12:59 PM

I defined the p as "double p;" forgot writing here because I am using phone.
I was expecting from the program give me exact golden ratio. I mean the Last two numbers' division. Like n/n-1

This post has been edited by Skydiver: 23 December 2016 - 07:36 AM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

### #4 jimblumberg

Reputation: 5441
• Posts: 16,966
• Joined: 25-December 09

## Re: Finding Golden ratio in fibonacci series

Posted 21 December 2016 - 01:03 PM

Quote

I was expecting from the program give me exact golden ratio.

Which is? Also what exactly is your program outputting?

Do you realize that when you divide two integers you get an integer result, meaning no fractions? 1 / 10 would yield zero.

Now, knowing that look at your line 18, that calculation produces an integer value that is implicitly converted to a double for the assignment.

Jim

This post has been edited by jimblumberg: 21 December 2016 - 01:03 PM

Reputation: 0
• Posts: 71
• Joined: 04-November 16

## Re: Finding Golden ratio in fibonacci series

Posted 21 December 2016 - 01:22 PM

jimblumberg, on 21 December 2016 - 01:03 PM, said:

Quote

I was expecting from the program give me exact golden ratio.

Which is? Also what exactly is your program outputting?

Do you realize that when you divide two integers you get an integer result, meaning no fractions? 1 / 10 would yield zero.

Now, knowing that look at your line 18, that calculation produces an integer value that is implicitly converted to a double for the assignment.

Jim

You mean like that
P=(double)a/b; ?

It is still giving 0

jimblumberg, on 21 December 2016 - 01:03 PM, said:

Quote

I was expecting from the program give me exact golden ratio.

Which is? Also what exactly is your program outputting?

Do you realize that when you divide two integers you get an integer result, meaning no fractions? 1 / 10 would yield zero.

Now, knowing that look at your line 18, that calculation produces an integer value that is implicitly converted to a double for the assignment.

Jim

I think the program is getting the first value of a and b that's why it is giving 0. How can I make it take the last value of a and b ?

### #6 jimblumberg

Reputation: 5441
• Posts: 16,966
• Joined: 25-December 09

## Re: Finding Golden ratio in fibonacci series

Posted 21 December 2016 - 01:24 PM

Quote

You mean like that

Yes.

Quote

It is still giving 0

So where exactly did you declare your variable p or is it now P?

And what exactly do the variables a and b contain when you do that assignment?

Did you try printing out all the variables in your loop to see what they contain in each iteration of the loop?

Jim

Reputation: 0
• Posts: 71
• Joined: 04-November 16

## Re: Finding Golden ratio in fibonacci series

Posted 21 December 2016 - 01:28 PM

jimblumberg, on 21 December 2016 - 01:24 PM, said:

Quote

It is still giving 0

So where exactly did you declare your variable p or is it now P?

And what exactly do the variables a and b contain when you do that assignment?

Did you try printing out all the variables in your loop to see what they contain in each iteration of the loop?

Jim

It is not P it is p.

What do you mean with "Did you try printing out all the variables in your loop to see what they contain in each iteration of the loop?" Is a in here last value or first value I am really confused

### #8 jimblumberg

Reputation: 5441
• Posts: 16,966
• Joined: 25-December 09

## Re: Finding Golden ratio in fibonacci series

Posted 21 December 2016 - 04:12 PM

Quote

What do you mean with "Did you try printing out all the variables in your loop to see what they contain in each iteration of the loop?"

I mean place a printf() statement inside the loop that prints each of the variables as the loop executes.

Quote

It is not P it is p.

You still didn't answer the main question: Where did you declare that variable? And how??

Did you print the values of a and b after your loop? Are a and b the values you should be using to compute your "Golden Ratio"?

Jim

### #9 CTphpnwb

• D.I.C Lover

Reputation: 3777
• Posts: 13,674
• Joined: 08-August 08

## Re: Finding Golden ratio in fibonacci series

Posted 22 December 2016 - 08:30 AM

Functions make writing code easier. The code below is C++, but you should be able to convert it to C easily if you know how to dynamically allocate memory. Another possibility would be to set a fixed size array. Using long integers (64 bits) you can't go beyond fibonacci(92) anyway!
```long fibonacci(long num) {
static vector<long> fibs;
if(fibs.size() < 2) {
fibs.push_back(0);
fibs.push_back(1);
}
for(size_t i = fibs.size(); i <= num; i++) {
fibs.push_back(fibs[i-1]+fibs[i-2]);
}
return fibs[num];
}

```

I'll leave it to you to figure out the ratio.

Reputation: 0
• Posts: 71
• Joined: 04-November 16

## Re: Finding Golden ratio in fibonacci series

Posted 22 December 2016 - 08:35 AM

I won't be able to convert it. I have already wrote codes of "how to find Fibonacci". I need to write golden ratio...

This post has been edited by Skydiver: 23 December 2016 - 07:35 AM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

### #11 CTphpnwb

• D.I.C Lover

Reputation: 3777
• Posts: 13,674
• Joined: 08-August 08

## Re: Finding Golden ratio in fibonacci series

Posted 22 December 2016 - 08:54 AM

Your code to find the fibonacci number is cryptic and confusing. That's why you're having trouble calculating a simple ratio!

Use descriptive variable names. Single characters have no meaning, so they make code difficult to read.

Use functions. Functions break up code into manageable components. You wouldn't try to build a house all at once, right? You'd build a frame before adding electrical wiring and plumbing, then insulation, and sheet rock. Each of these is a separate component, or function.

Reputation: 0
• Posts: 71
• Joined: 04-November 16

## Re: Finding Golden ratio in fibonacci series

Posted 22 December 2016 - 12:29 PM

jimblumberg, on 21 December 2016 - 12:54 PM, said:

Quote

I wrote this code but it gives golden ratio as "0"

And what exactly did you expect the program to output? What did you input into the program?

By the way where did you define the variable p?

Jim

Now I have got some sensible output. But still it is wrong, it is supposed to give 1.6180

Reputation: 0
• Posts: 71
• Joined: 04-November 16

## Re: Finding Golden ratio in fibonacci series

Posted 22 December 2016 - 12:34 PM

CTphpnwb, on 22 December 2016 - 08:54 AM, said:

Your code to find the fibonacci number is cryptic and confusing. That's why you're having trouble calculating a simple ratio!

Use descriptive variable names. Single characters have no meaning, so they make code difficult to read.

Use functions. Functions break up code into manageable components. You wouldn't try to build a house all at once, right? You'd build a frame before adding electrical wiring and plumbing, then insulation, and sheet rock. Each of these is a separate component, or function.

Here is my new output it is giving wrong again...

### #14 CTphpnwb

• D.I.C Lover

Reputation: 3777
• Posts: 13,674
• Joined: 08-August 08

## Re: Finding Golden ratio in fibonacci series

Posted 22 December 2016 - 04:24 PM

It's nice to have your output, but that is far less important than your code. Always remember that we can only see the limited information you provide.

By the way, if you know anything about recursion, you could easily make a recursive function to do this. It would be slower than what I've shown, but it would work.

This post has been edited by CTphpnwb: 22 December 2016 - 04:29 PM

### #15 baavgai

• Dreaming Coder

Reputation: 7150
• Posts: 14,890
• Joined: 16-October 07

## Re: Finding Golden ratio in fibonacci series

Posted 23 December 2016 - 07:27 AM

So, instead of being 1.6180, it's closer to 0.6180?

```n = 20
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181
a = 6765
b = 10946
(double)a/b = 0.618034
(double)b/a = 1.618034

```

Hope this helps.