C Program to Compute First N Fibonacci Numbers using Command Line Arguments

This C Program computes first N fibonacci numbers using command line arguments.

Here is source code of the C Program to compute first N fibonacci numbers using command line arguments. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Compute First N Fibonacci Numbers using Command Line Arguments
  3.  */
  4. #include <stdio.h>
  5.  
  6. /* Global Variable Declaration */
  7. int first = 0;
  8. int second = 1;
  9. int third;
  10. /* Function Prototype */
  11. void rec_fibonacci(int);
  12.  
  13. void main(int argc, char *argv[])/* Command line Arguments*/
  14. {
  15.     int number = atoi(argv[1]);
  16.     printf("%d\t%d", first, second); /* To print first and second number of fibonacci series */
  17.     rec_fibonacci(number);
  18.     printf("\n");
  19. }
  20.  
  21. /* Code to print fibonacci series using recursive function */
  22. void rec_fibonacci(int num)
  23. {
  24.     if (num == 2)    /* To exit the function as the first two numbers are already printed */
  25.     {
  26.         return;
  27.     }
  28.     third = first + second;
  29.     printf("\t%d", third);
  30.     first = second;
  31.     second = third;
  32.     num--;
  33.     rec_fibonacci(num);
  34. }

$ cc arg6.c
$ a.out 10
0       1       1       2       3       5       8       13      21      34

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.