Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

Nth Fibonacci Number

Nth Fibonacci Number using Command Line Arguments

Using Command Line Arguments

#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;
    }
}