Thursday, December 3, 2015

Fibonaccis Series

Program #1:
Fibonaccis Series

#include<stdio.h>
#include<conio.h>
int main(){
 int i,j,k;
 clrscr();
 printf("Please enter Nth value for Fibonacci's Series:\n");
 scanf("%d", &k);
 /* Validate input data */
 if (k > 24 || k < 3) {
   printf("Please enter validate data next time!!\nBye c u later");
   getch();
   return 0;
 }
 /*
 Initialize first two for Fibonaccis series values
 */
 i = 0;
 j = 1;
 printf("\nSeries 1:0");
 printf("\nSeries 2:1");
 for (int m=3; m<=k; m++){
   int r = i+j;
   i = j;
   j = r;
   printf("\nSeries %d:%d", m,r);
 }
 getch();
 return 0;
}

Explanation:
If users gives Fibonaccis series value greater than 24 then the result will cross 32768.  What 32768? it means maximum limit of int storage limit.  Because currently i, j and r variables as integer.  Integer storage limit is 2 byte i.e. 2 to the power 15 i.e. 32768.

If you notice, variable m and r are not declared on top of the program.  But how it is working?  In C program, it is not mandatory to declare the variables on top of the program.  You can declare it where ever you wish.  So, in this program declared inside the code.

If user gives, Fibonaccis series value as 28 (for example) then program will display error message and quit the execution using return 0.  So, how a C program can have two return 0??.  Yes it is possible.  You can add as many return 0 as you wish.  It will quit and stop the execution.

No comments:

Post a Comment

Popular Posts