Search This Blog

About Me

My photo
Hello, my name is Gurnoor Singh. I am the creator of NoorEDU, a blog for about education . I live in India. I study in BCA Trade.In my free time, I enjoy hiking, practicing photography, and exploring the city by bike.

Wednesday 9 September 2020

C Program for Fibonacci Series using While Loop & For 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

  1. #include<stdio.h>
  2. main()
  3. {
  4. int n, first = 0, second = 1, next, c;
  5. printf("Enter the number of terms\n");
  6. scanf("%d",&n);
  7. printf("First %d terms of Fibonacci series are :-\n",n);
  8. for ( c = 0 ; c < n ; c++ )
  9. {
  10. if ( c <= 1 )
  11. next = c;
  12. else
  13. {
  14. next = first + second;
  15. first = second;
  16. second = next;
  17. }
  18. printf("%d\n",next);
  19. }
  20. return 0;
  21. }
  22.  

0 comments:

Post a Comment

Bitwise Operator in C