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;
        printf("%d ", curr);
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.


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.