I am afraid you need to really clean up your code.The following is absolutely wrong in C.
(int i = 0; i < n; i++)
I am sure you intend a for
loop here.But who knows what you had in mind? Then you have enclosed a solitary else
in the if
block in Fibonacci()
.There are mistakes galore in your program, so let me give you a program that creates the Fibonacci series based on how many numbers in the series the user wants (choice entered from keyboard).Please try to understand this program.It’s very easy as the logic behind Fibonacci series is simple itself.It just that it’s obvious you are very new to the C language.
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i,l,x=1,y=1,z;
printf("Enter how many Fibonacci numbers you want\n");
scanf("%d",&l);
printf("The Fibonacci series of %d numbers is\n",l);
if(l==1)
{
printf("1");
exit(0);
}
if(l==2)
{printf("1+1");
exit(0);
}
printf("1+1+");
for(i=3;i<=l;i++)
{
z=x+y;
printf("%d",z);
x=y;
y=z;
if((i+1)<=l) printf("+");
}
}
Since you say you’ve implemented this series in other languages, you won’t have problem started the series as 0,1,1,2,3,5.....
, My program goes 1,1,2,3,5...
as initialized both x
and y
to 1
instead of initializing x
as 0
.Tweak the code to achieve it.
solved Fibonacci Series in C – The series of numbers up to a given number [closed]