I am writing a C program to get Fibonacci number, the user needs to put the first 2 numbers and the sequence starts from there. Here is my code:
#include <stdio.h>
#define MAX_SIZE 100
int main()
{
int i, input[MAX_SIZE];
printf("please Enter first 2 digit of the Sequence\n");
scanf("%d, %d" , &input[0], &input[1]);
for (i = 2; i < MAX_SIZE; i++)
{
input[i] = input[i-2] + input[i-1];
printf("%d\n", input[i]);
}
return 0;
}
But when i run the code with a input 2 and 3, I get a output like this 1499141456
, which is clearly not the sequence. please help.
scanf
. If it's not 2, then it did not read two numbers, and the ones it didn't read are undefined (and could be anything). Yourscanf
format is%d, %d
, which means it is requiring a comma between the two numbers. If you don't enter a comma when prompted, it will not parse the second number. – Tom Karzes Oct 20 '17 at 19:21