Runescapian321
2008-05-18 11:29:31
I'm supposed to 'Write a program that reads a positive integer n entered by the user and then computes and displays the corresponding Fibonacci number"
Here's what I have so far
It works fine for up to 4, but then at 5 it starts messing up. I've been trying to get it to work for about an hour now (from scratch I mean) and my head hurts O_o Any hints or tips would be appreciated…
Here's what I have so far
/* This program displays the fibonacci number of the user's input*/
#include "stdafx.h"
#include "simpio.h"
#include "genlib.h"
#include <math.h>
int fibonacci(int num);
int main()
{
int input, fibnum;
printf("Input a positive integer to calculate the fibonacci of: ");
input = GetInteger();
fibnum=fibonacci(input);
printf("f(%d) = %d\n", input, fibnum);
}
int fibonacci(int num)
{
int num1, num2, fibnum, i;
num1 = 0;
num2 = 1;
if (num == 0)
{
return(0);
}
if (num == 1)
{
return(1);
}
i = 1;
while (i != num)
{
fibnum = num1+num2;
num1 = num1+num2;
i+=1;
while (i != num)
{
fibnum = num1+num2;
num2=num1 + num2;
i+=1;
}
}
return(fibnum);
}
It works fine for up to 4, but then at 5 it starts messing up. I've been trying to get it to work for about an hour now (from scratch I mean) and my head hurts O_o Any hints or tips would be appreciated…