//To print the Fibonacci Series upto a certain number //@author-Karan Narain(karan@iitk.ac.in) #include int main() { int n,a,b,c=0,i; /*The while loop below is included to satisfy the requirement that the inputs need to be positive.The while loop keeps repeating till the user enters valid inputs.To enter into this while loop the first time,we initialize n to an illegal value(-1).We maintain a counter i which counts the number of times the user has entered values and display a message accordiingly*/ n=-1; i=0; while(n<=0) { //This is the first time the user is entering the value if(i==0) printf("\nPlease enter a positive value for n\n"); //The user had entered an incorrect value.Print appropriate message else printf("The value of n needs to be positive.Please enter a proper value\n"); scanf("%d",&n); i++; } a=1; b=1; printf("The Fibonacci series till %d is as follows\n",n); printf("%d,%d",a,b); c=a+b; while(c<=n) { printf(",%d",c); a=b; //F(n-2)=F(n-1) b=c; //F(n-1)=F(n) c=a+b; //Compute the new F(n) } printf("\n"); return 0; }