Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- void main()
- {
- long int n, a, b, c;
- int i, choice;
- printf("This is a simple program to generate Fibonacci Series.\n\n");
- printf("Choose an option:\n");
- printf("1 - Generate Fibonacci numbers up to a number (<= N)\n");
- printf("2 - Generate first N Fibonacci numbers\n\n");
- printf("Enter your choice : ");
- scanf("%d", &choice);
- if(choice == 1)
- {
- printf("\nEnter the maximum value (N) : ");
- scanf("%ld", &n);
- a = 0;
- b = 1;
- printf("\nFibonacci Series up to %ld :\n", n);
- while(a <= n)
- {
- printf("%ld ", a);
- c = a + b;
- a = b;
- b = c;
- }
- }
- else if(choice == 2)
- {
- printf("\nEnter number of terms (N) : ");
- scanf("%ld", &n);
- a = 0;
- b = 1;
- printf("\nFirst %ld terms of Fibonacci Series :\n", n);
- for(i = 1; i <= n; i++)
- {
- printf("%ld ", a);
- c = a + b;
- a = b;
- b = c;
- }
- }
- else
- {
- printf("\nInvalid choice. Please run the program again and enter 1 or 2.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement