1. ## Fibonacci

Hi everyone! This is my first post, and I'm quite a noob at C. I have a homework assingment I have to do, and I've thought on it a lot, but it's confusing me. Here's the exact problem given:

The Fibonacci number sequence is commonly used in mathematics and is commonly found in nature. Request that the user enter a number between 1 and 100 and have the program respond back which Fibonacci number corresponds. You must use loops and functions to solve this problem. Note, the formula can be found here:

Fibonacci number - Wikipedia, the free encyclopedia

For example the sequence is:

1.1
2.2
3.3
4.5
5.8
6.13... etc.
For example:

>>>Which Fibonacci number would you like to see?

>>>Fibonacci number 7 is 21.

I'm guessing I just start it off requesting the user for input and then the number they input be plugged into the formula F(n)=F(n-1)+F(n-2)=F(0)+F(1), but with this if I entered in 10, i'd have to know the Fibonacci numbers for 9 and 8? and it'd be that pattern all way up to 100? Really don't know how I'd do that =(. Can anyone help me here or give me an similar example of some sort? 2. So can you write a loop which generates the Fibonacci sequence?

Like the first 10 numbers are ... kind of thing?

Instead of 10, use the number that was input. 3. Just use a recursive function 4. Actually recursion is much slower than iteration for this. It's certainly shorter but not efficient. 5. It would appear that none of the standard datatypes have enough space/precision to store the largest numbers of first 100 Fibonacci numbers. E.g. 573`147`844`013`817`084`101 is the 100th Fibonacci number, but on my system, stored in a long double it is 573`147`844`013`817`084`096.

Suggestions?  6. Use multiple precision arithmetic library like gmp.
or just use bc calculator if you don't need it to be done in C. 7. Originally Posted by rob90 Just use a recursive function
If you're suggesting what I think you're suggesting, then I hope that was a joke.
The naieve recursive imlementation of it takes literally hours to calculate a mere fib(50). Reference
Besides, it says you must use loops. 8. I got it working with a recursive function, but uhm yeah...took forever, so I'll try to do it with the other thing. 9. You can do addition yourself manually using a large array of chars, e.g. Use the digits '0' to '9', do addition digit by digit with carry over the way you might do it by hand. It's not too difficult, I implemented fibonacci in 56 lines using this method, and it only takes 100 ms to calculate the 1000th number.  10. Originally Posted by iMalc If you're suggesting what I think you're suggesting, then I hope that was a joke.
The naieve recursive imlementation of it takes literally hours to calculate a mere fib(50). Reference
Besides, it says you must use loops.
I replaced what I had with the iteration function that was on your link. I'm getting an error that says: 'test.c: In function Fibonacci:' 'test.c:23: error: for loop initial declaration used outside C99 mode'

Code:
```

#include <stdio.h>

unsigned int Fibonacci (unsigned int n);

int main ()
{

long result;
long number;

printf("Enter an int: ");
scanf("%ld", &number);
result=Fibonacci(number);
printf("Fibonacci(%ld)=%ld/n",number,result);
return 0;
}

unsigned int Fibonacci (unsigned int n)
{
int previous = -1;
int result =1;
for (unsigned int i= 0; i <= n; ++i)
{
int const sum=result + previous;
previous = result;
result = sum;
}
return result;``` 11. The solution is simple: do not declare variables in the initial statement of a for loop, i.e., write:
Code:
```unsigned int i;
for (i = 0; i <= n; ++i)```
Code:
`for (unsigned int i= 0; i <= n; ++i)` 12. You're missing the closing curly-bracket of the for-loop as well.
Otherwise it's looking alright.
What do you get if you enter 100? 13. dang it. I feel like such a noob Code:
```#include <stdio.h>

unsigned int Fibonacci (unsigned int n);

int main ()
{

long result;
long number;

printf("Enter an int: ");
scanf("%ld", &number);
result=Fibonacci(number);
printf("Fibonacci(%ld)=%ld/n",number,result);
return 0;
}

unsigned int Fibonacci (unsigned int n)
{

int previous= -1;
int result =1;
unsigned int i;
for (unsigned int i=0; i <=n; ++i)
{
int const sum=result + previous;
previous= result;
result = sum;
}
}
return result;```
Now I'm getting...

test.c: In function âFibonacciâ:
test.c:25: error: redeclaration of âiâ with no linkage
test.c:24: error: previous declaration of âiâ was here
test.c:25: error: âforâ loop initial declaration used outside C99 mode
cc1: warnings being treated as errors
test.c:31: error: control reaches end of non-void function
test.c: At top level:
test.c:32: error: expected identifier or â(â before âreturnâ 14. Also, note that you are reading the input n in a long and passing it to a function that takes an unsigned int as a parameter. Do NOT mix up signed and unsigned variables. 15. My English is poor, so I post code directly

Code:
```#include <stdio.h>

int main(void)

{

int F;

int i;

F = 0; F = 1;

for (i = 2; i <= N; i++)

F[i] = F[i-1] + F[i-2];

printf("%d\n", F);

return 0;

}``` Popular pages Recent additions 