Advertisement
Realratnadwip

Fibonacci Series [Both]

May 21st, 2025 (edited)
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.21 KB | Source Code | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. void main()
  5. {
  6.     long int n, a, b, c;
  7.     int i, choice;
  8.  
  9.     printf("This is a simple program to generate Fibonacci Series.\n\n");
  10.     printf("Choose an option:\n");
  11.     printf("1 - Generate Fibonacci numbers up to a number (<= N)\n");
  12.     printf("2 - Generate first N Fibonacci numbers\n\n");
  13.     printf("Enter your choice : ");
  14.     scanf("%d", &choice);
  15.  
  16.     if(choice == 1)
  17.     {
  18.         printf("\nEnter the maximum value (N) : ");
  19.         scanf("%ld", &n);
  20.  
  21.         a = 0;
  22.         b = 1;
  23.  
  24.         printf("\nFibonacci Series up to %ld :\n", n);
  25.         while(a <= n)
  26.         {
  27.             printf("%ld ", a);
  28.             c = a + b;
  29.             a = b;
  30.             b = c;
  31.         }
  32.     }
  33.     else if(choice == 2)
  34.     {
  35.         printf("\nEnter number of terms (N) : ");
  36.         scanf("%ld", &n);
  37.  
  38.         a = 0;
  39.         b = 1;
  40.  
  41.         printf("\nFirst %ld terms of Fibonacci Series :\n", n);
  42.         for(i = 1; i <= n; i++)
  43.         {
  44.             printf("%ld ", a);
  45.             c = a + b;
  46.             a = b;
  47.             b = c;
  48.         }
  49.     }
  50.     else
  51.     {
  52.         printf("\nInvalid choice. Please run the program again and enter 1 or 2.");
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement