#include<stdio.h>
int main()
{
int first=0, second=1, i, n, sum=0;
printf("Enter the number of terms: ");
scanf("%d",&n);
//accepting the terms
printf("Fibonacci Series:");
for(i=0 ; i<n ; i++)
{
if(i <= 1)
{
sum=i;
}
//to print 0 and 1
else
{
sum=first + second;
first=second;
second=sum;
//to calculate the remaining terms.
//value of first and second changes as new term is printed.
}
printf(" %d",sum)
}
return 0;
}
>>> def fib(n): # write Fibonacci series up to n
... """Print a Fibonacci series up to n."""
... a, b = 0, 1
... while a < n:
... print(a, end=' ')
... a, b = b, a+b
... print()
...
>>> # Now call the function we just defined:
... fib(2000)
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597
1, 1, 2, 3, 5, 8
#include <stdio.h>
void main()
{
int s1=0,s2=1; //initializing first two numbers
int nextNum=0,SumUpto=0;
printf("\n\n\tPlease enter number up to which print Fibonacci series is required \t");
scanf("%d",&SumUpto);
//here assuming user will enter value more than 1
//printing first two numbers
printf("\n\tfibbonacci Series up to %d is ",SumUpto);
printf("\n\n\t%d %d",s1,s2);
for(nextNum=2;nextNum<=SumUpto;)
{
s1=s2;
s2=nextNum;
printf(" %d",nextNum);
nextNum=s1+s2;
}
}