spngegirl01
asked on
How Do I Get This Fibonacci Program to Accept User Input?
I am trying to develop a program that takes in a user input of a number and returns as an output that number in the fibonacci series. For example, if the user types in 5, the 5th number is displayed; 23 and the 23rd number is displayed. So far I have this, but it only spits out the fibonacci series for 100 numbers and does not stop at the number put in by the user. Please help!
#include <stdio.h>
char line[1000];
int main()
{
int fib[1000];
int i;
printf("Fibonacci\n");
fgets(line, sizeof(line), stdin);
sscanf(line," %d", &i);
if (i<0) return 0;
fib[0] = 0;
fib[1] = 1;
for (i = 2; i<1000; i++)
{
fib[i] = fib[i-1] + fib[i-2];
}
for (i = 0; i<1000; i++)
{
printf("%d %d\n", i, fib[i]);
}
return (0);
}
#include <stdio.h>
char line[1000];
int main()
{
int fib[1000];
int i;
printf("Fibonacci\n");
fgets(line, sizeof(line), stdin);
sscanf(line," %d", &i);
if (i<0) return 0;
fib[0] = 0;
fib[1] = 1;
for (i = 2; i<1000; i++)
{
fib[i] = fib[i-1] + fib[i-2];
}
for (i = 0; i<1000; i++)
{
printf("%d %d\n", i, fib[i]);
}
return (0);
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Sorry sunnycoder,
I was not aware that it was homework problem
I was not aware that it was homework problem
ASKER
fib[0] ='\0';
fib[1] = 1;
fib[2] = 1;