Home / Programs / Nth Fibonacci Number using Command Line Arguments
Programming Example

Nth Fibonacci Number using Command Line Arguments

👁 585 Views
💻 Practical Program
📘 Step by Step Learning
Nth Fibonacci Number using Command Line Arguments

Program Code

#include<stdio.h>
#include<stdlib.h>

int fib(int n)
{
    int a=0,b=1,c,i;
    
    if(n==0) return a;
    for(i=2;i<=n;i++)
    {
        c=a+b;
        a=b;
        b=c;
    }
return b;
}


int main(int argc, char * argv[])
{
    if(argc==1)
    {
        printf("No arguments");
        return 0;
    }
    else
    {
        int n;
        n=atoi(argv[1]);
        printf("%d",fib(n));
    return 0;
    }
}

Output

10

55

Explanation

Nope

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.