C Program to Generate Fibonacci Series of N Numbers using Command-Line Argument

This C Program generates fibonacci series of n numbers using command-line argument.
Here is source code of the C Program to generate fibonacci series of n numbers using command-Llne argument. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
  1. /*  
  2.  * C Program to Generate Fibonacci Series of N Numbers using 
  3.  * Command-Line Argument
  4.  */
  5. #include <stdio.h>
  6.  
  7. void main(int argc, char * argv[])
  8. {
  9.     int n, last = 0, prev = 1, curr, cnt;
  10.     n = atoi(argv[1]);
  11.     printf("Printing first %d fibonacci nos. -> ", n);
  12.     printf("%d ", last);
  13.     printf("%d ", prev);
  14.     cnt = 2;
  15.     while (cnt< = n-1)
  16.     {
  17.         curr = last + prev;
  18.         last = prev;
  19.         prev = curr;
  20.         cnt++;
  21.         printf("%d ", curr);
  22.     }
  23.     printf("\n");
  24. }

$ gcc arg5.c
$ a.out 10
Printing first 10 fibonacci nos. -> 0 1 1 2 3 5 8 13 21 34

0 comments:

Post a Comment

 
Top