# C Program to Generate Fibonacci Series using Command Line Argument

This is a C program to generate fibonacci series of n numbers using command-line argument.

Problem Description

This C Program generates fibonacci series of n numbers using command-line argument.

Problem Solution

It displays fibonacci series of n numbers using command-line argument as shown in the program below.

Program/Source Code

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");
}```
Program Explanation

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.

Runtime Test Cases
```
\$ 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.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

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.