Fibonacci Series||C language||programming_info ||Series problem 1

Fibonacci Series



 Solution using C language:
  1. #include<stdio.h>
  2. int main(){
  3. int i,range;
  4. long int arr[40];
  5.  
  6. printf("Enter the number range: ");
  7. scanf("%d",&range);
  8.  
  9. arr[0]=0;
  10. arr[1]=1;
  11.  
  12. for(i=2;i<range;i++){
  13. arr[i] = arr[i-1] + arr[i-2];
  14. }
  15.  
  16. printf("Fibonacci series is: ");
  17. for(i=0;i<range;i++)
  18. printf("%ld ",arr[i]);
  19. return 0;
  20. }
  21. Output:
  22. Enter the number range: 20
    Fibonacci series is: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181
    Alternate Solution using While loop in c:
  23. #include<stdio.h>
  24. int main(){
  25. int k=2,r;
  26. long int i=0l,j=1,f;
  27. printf("Enter the number range:");
  28. scanf("%d",&r);
  29.  printf("Fibonacci series is: %ld %ld",i,j);
  30.  
  31. while(k<r){
  32. f=i+j;
  33. i=j;
  34. j=f;
  35. printf(" %ld",j);
  36. k++;
  37. }
  38. return 0;
  39. }
  1. Output:
  2. Enter the number range: 10
    Fibonacci series is: 0 1 1 2 3 5 8 13 21 34
  1. Alternate Solution using recursion in c:
  2. #include <stdio.h>
  3. #include <conio.h>
  4. unsigned long fib(int);
  5. void main()
  6. {
  7. int n,i;
  8. unsigned long f;
  9. clrscr();
  10. printf("\nENTER A NUMBER: ");
  11. scanf("%d",&n);
  12. printf("\nTHE FIBONNACI SERIES UPTO %d NUMBERS IS:\n",n);
  13. for(i=0;i<n;i++)
  14. {
  15. f=fib(i);
  16. printf("%lu ",f);
  17. }
  18. getch();
  19. }
  20. unsigned long fib(int x)
  21. {
  22. unsigned long res;
  23. if(x==0)
  24. return(0);
  25. else
  26. if(x==1)
  27. return(1);
  28. else
  29. {
  30. res=fib(x-1)+fib(x-2);
  31. return(res);
  32. }
  33. }
  34. Output:
  35. ENTER A NUMBER: 10
    THE FIBONNACI SERIES UPTO 10 NUMBERS IS
    0 1 1 2 3 5 8 13 21 34
Share:

No comments:

Post a Comment

Translate

Recommended platforms

  1. codechef
  2. hackerrank
  3. codeforces
  4. leetcode
  5. hackerearth

Popular Posts

programming_info. Powered by Blogger.

Blog Archive

Recent Posts

other platforms

  • geeks for geeks
  • w3schools
  • codepen
  • skillshare
  • udemy

Pages

reader support Support