C Program for Fibonacci Series using While Loop
C program for Fibonacci series up to given length.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include<stdio.h> #include<conio.h> main() { int f1=0,f2=1,f3,i=3,len; printf ( "enter length of the fibonacci series:" ); scanf ( "%d" ,&len); printf ( "%d\t%d" ,f1,f2); // It prints the starting two values while (i<=len) // checks the condition { f3=f1+f2; // performs add operation on previous two values printf ( "\t%d" ,f3); // It prints from third value to given length f1=f2; f2=f3; i=i+1; // incrementing the i value by 1 } getch(); } |
Output for Fibonacci Series Program
enter length of the fibonacci series:10
0 1 1 2 3 5 8 13 21 34
Fibonacci series using loop in C
- #include<stdio.h>
- main()
- {
- int n, first = 0, second = 1, next, c;
- printf("Enter the number of terms\n");
- scanf("%d",&n);
- printf("First %d terms of Fibonacci series are :-\n",n);
- for ( c = 0 ; c < n ; c++ )
- {
- if ( c <= 1 )
- next = c;
- else
- {
- next = first + second;
- first = second;
- second = next;
- }
- printf("%d\n",next);
- }
- return 0;
- }
0 comments:
Post a Comment