can you check the code... it doesn't work for some reason i tryied everything

#include <stdio.h>
#include "genlib.h"
#include "simpio.h"

int Fibonacci(int fnum);

int main()
{
	int fnum;

	printf("Please enter in the Fibonacci number: ");
	fnum = GetInteger();
	printf("The number for f(%d) is %d\n", fnum, Fibonacci(fnum));
	system("pause");
}

int Fibonacci(int fnum)
{
	int num1, num2, num3, fib, n1;
	num1 = 0;
	num2 = 1;
	for (fib=1; fib=num2 + 1; fib++)
	{
		num3 = num1;
		num1 = num1 + num2;
		num2 = num3;
		if(fib = fnum)
		{
			break;
		}
		else;
	}
	return(num1);
}

Recommended Answers

All 4 Replies

if(fib = fnum)

Try ==. = is for assignment, == is for comparison. (Your compiler should warn you about this if you enable warnings.)

Plus I don't think this is what you want.

for (fib=1; fib=num2 + 1; fib++)

The comparison portion of that for loop is actually an assignment, and an assignment that always yields the same value.

Also, system() is in <stdlib.h>.

[edit] BTW, if you want your signature to look anything like it should, put it in code tags. [/edit]

[...] i tryied everything

No everything.

int Fibonacci(int fnum)
{
	int num1, num2, num3, fib; /* deleted n1, not needed */
	num1 = 1; /* replaced 0 by 1 */
	num2 = 1;
        /* changes in the loop: */
        /*  fig = 3 instead of fib=1 */
        /*  fig <= fnum instead of fib=num2 + 1 */
	for (fib = 3; fib <= fnum; fib++)
	{
		num3 = num2; /* num2 instead of num1 */
		num2 = num1 + num2; /* lvalue num2 instead of num1 */
		num1 = num3; /* lvalue num1 instead of num2 */
                
               /* byebye if/else statements */
        }
		
	return(num2); /* return num2 instead of num1 */
}
/* replaced 0 by 1 */

Depending on who you ask, the first fibonacci number can be 0 or 1. (Personally, I'd agree with you and have it 1.)

Why not use more descriptive names than num1, num2, etc?

/* replaced 0 by 1 */

Depending on who you ask, the first fibonacci number can be 0 or 1. (Personally, I'd agree with you and have it 1.)

Ah, now I know what I did wrong. I did not consult with who.
Don't read too much into it. ;)

Why not use more descriptive names than num1, num2, etc?

I was afraid that it would turn into something like this:

int fibonacci(int num)
{
  return (num < 2 ? num : fibonacci(num-1) + fibonacci(num-2));
}

And then I would not been able to use all those nice green /*comments */ . I like green.
Again, don't read too much into it.

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.