1. ## Help for loops

ok so im suppose to use fibonicci number

"The sequence of integers 1,1,2,3,5,8,13....has been name the fibonacci sequence in honor of mathematician. the first two fibonacci numbers (0 and 1st) are both1; thereaftereach number in the sequence is the sum of the two preceding numbers. Write a program that prompts the user for an integer,m,then prints the mth fibonacci number."

Code:
```/* Fibonacci Numbers
Daniel Latorre
CSCI 143 */

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

int main()
{
int fibo,m,c;
printf("give me m: ");
scanf("%i",&m);

if(m<0){
printf("Enter an appropiate positive integer");
}
else {
for(c=0;c<m;c++)
{

}

printf("The %i Fibonacci number is: %i",m,fibo);```
i havent done much after that cause im stuck in the part of the equation.....how can i make the for loop add the sum of the previous numbers?

any help is appreciated

2. Well the problem statement has your answer. From the definition of the fibonacci number, you have: F(1) = 1, F(2) = 1, F(N) = F(N-1) + F(N-2).
Therefore, you need two variables to track the two previous numbers. You need a variable to track the current number that has been calculated, and should that reach your 'm', the for loop ends.
(I suppose you need to do it with a for loop as an assignment, as a while loop would be cleaner and easier to write for this problem)
And at every step of the loop after F(k) has been calculated it's simple to proceed as F(K+1) = F(K) + F(K-1), which means that the variable that held F(K-2) needs to contain what the variable that held F(K-1) has.

Or you could use a recursive function which maps the problem and the solution of calculating a fibonacci number more closely than any non-recursive algorithm does.

3. You'll have to keep the two previous numbers around in order to add them.

4. Originally Posted by xuftugulus
Or you could use a recursive function which maps the problem and the solution of calculating a fibonacci number more closely than any non-recursive algorithm does.
To save iMalc the trouble: the iterative version can find F_1000 in 1000 steps; the recursive version requires F_1000 = 1 billion+ steps.

5. Yes i know, but it's the shortest code ever! LOL!

6. Originally Posted by xuftugulus
Well the problem statement has your answer. From the definition of the fibonacci number, you have: F(1) = 1, F(2) = 1, F(N) = F(N-1) + F(N-2).
Therefore, you need two variables to track the two previous numbers. You need a variable to track the current number that has been calculated, and should that reach your 'm', the for loop ends.
(I suppose you need to do it with a for loop as an assignment, as a while loop would be cleaner and easier to write for this problem)
And at every step of the loop after F(k) has been calculated it's simple to proceed as F(K+1) = F(K) + F(K-1), which means that the variable that held F(K-2) needs to contain what the variable that held F(K-1) has.

Or you could use a recursive function which maps the problem and the solution of calculating a fibonacci number more closely than any non-recursive algorithm does.
so i have a question should i implement a if statement, just so that the first two numbers are left alone, and then put the equation in for the loop as the else statement?

7. Originally Posted by dals2002
so i have a question should i implement a if statement, just so that the first two numbers are left alone, and then put the equation in for the loop as the else statement?
I can't see why you would want to. If m is 0 or 1, your for loop won't run (assuming you've set up your for loop correctly), so just let it go.

8. Originally Posted by tabstop
I can't see why you would want to. If m is 0 or 1, your for loop won't run (assuming you've set up your for loop correctly), so just let it go.
why wouldn't i run it in 1 and 0? the fibonacci rule says that when the fibo number is 1 then its equal to 1, and as stated in the problem the number 0 must be 1 too

9. Originally Posted by dals2002
why wouldn't i run it in 1 and 0? the fibonacci rule says that when the fibo number is 1 then its equal to 1, and as stated in the problem the number 0 must be 1 too
And since that's how it's defined, your for loop doesn't start until you get to n=2.

10. Fibonacci numbers are so funny.
~/Projects/test\$ ./fib 40
Recursively Fib_40, bye bye stack!: 102334155 in 204668309 calls.
I will try to find the formula for the exact number of calls when calculating fibonacci numbers recursively.
Originally Posted by dals2002:
why wouldn't i run it in 1 and 0? the fibonacci rule says that when the fibo number is 1 then its equal to 1, and as stated in the problem the number 0 must be 1 too
Code:
`         for(c=0;c<m;c++)`
Hint: Since 0 and 1 are known, why start the loop at 0 or 1?
Just initialize your 'fibo' variable to 1, and start the loop from 2.
The for loop will test the condition before entering, and if m is 0, or 1 will not go off.
Which of course means that the loop should go off for m==2.

11. thanks guys i finally got it, it took some time and understanding but atleast i got it, this is my final program for those that want to look at it

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

int main(void) {
int n,i,c=1,n2=1,two;

printf("How many Fibonacci numbers do you want to compute? ");
scanf("&#37;d", &n);
if (n<=0)
printf("The number should be positive.\n");
else {
printf("\n\n\tInteger Fibonacci(Int)\n\t=====================\n");

for (i=0; i<=n; i++) {
printf("%12d %12d\n", i, c);
two=c+n2;
c=n2;
n2=two;
}
}
getchar();
getchar();
}```

Some post notes.

You don't need <math.h>, even though Fibonacci was a mathematician

Try to keep statements indented based on the level of braces ( block level we call it ).
Code:
```int main()
{
int x = 1; /* Level 1, 1 tab, 4 spaces */
if(x == 1)
{
for(x = 0; x<10; x++) /* Level 2, 2tabs, 8 spaces */
{
printf("&#37;d\n", x);  /* Level 3, 3tabs, 12 spaces */
}
}
}```
Makes other people understand just by looking at which level statements are on.
Most people will agree it looks nice too.

Good luck, generally

13. Originally Posted by dals2002
thanks guys i finally got it, it took some time and understanding but atleast i got it, this is my final program for those that want to look at it

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

int main(void) {
int n,i,c=1,n2=1,two;

printf("How many Fibonacci numbers do you want to compute? ");
scanf("%d", &n);
if (n<=0)
printf("The number should be positive.\n");
else {
printf("\n\n\tInteger Fibonacci(Int)\n\t=====================\n");

for (i=0; i<=n; i++) {
printf("%12d %12d\n", i, c);
two=c+n2;
c=n2;
n2=two;
}
}
getchar();
getchar();
}```
why did ya write n2=two,can u tell???