This is a 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.
It displays fibonacci series of n numbers using command-line argument as shown in the program below.
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.
/* * C Program to Generate Fibonacci Series of N Numbers using * Command-Line Argument */ #include <stdio.h> void main(int argc, char * argv[]) { int n, last = 0, prev = 1, curr, cnt; n = atoi(argv[1]); printf("Printing first %d fibonacci nos. -> ", n); printf("%d ", last); printf("%d ", prev); cnt = 2; while (cnt< = n-1) { curr = last + prev; last = prev; prev = curr; cnt++; printf("%d ", curr); } printf("\n"); }
In this C program, we are computing first N Fibonacci numbers using command line arguments. The arguments argc and *argv[] are used. Initially assign the first variable value as 0 and second variable value as 1.
The rec_fibonacci() function is used to compute the Fibonacci series. If condition statement is used to check the value of ‘num’ variable is equal to 2. If the condition is true then exit the function. Print the statement as the first two numbers are already printed.
If the condition is false then execute the else statement. Compute the value of ‘first’ and ‘second’ variable. Assign to third variable and print the Fibonacci series. Then the value of ‘second’ variable is assigned to the value of ‘first’ variable and the value of ‘third’ variable is assigned to ‘second’ variable and decrement the value of ‘num’ variable.
$ gcc arg5.c $ a.out 10 Printing first 10 fibonacci nos. -> 0 1 1 2 3 5 8 13 21 34
Sanfoundry Global Education & Learning Series – 1000 C Programs.
Here’s the list of Best Books in C Programming, Data-Structures and Algorithms
- Practice Computer Science MCQs
- Practice BCA MCQs
- Apply for C Internship
- Check Computer Science Books
- Watch Advanced C Programming Videos