Assignment 1

3. Ask the user for the value of N and output the nth term of the fibonacci series

CPE102L
1Q1819
#include <iostream>
using namespace std;
int main()
{
 int a=0, b=1, ctr=1, n;
 int fib;
 
 cin>>n;
 
 if (n<0)
 {
  cout<<"out of range";
 }
 else if(n==1)
 {
  cout<<a;
 }
 else if(n==2)
 {
  cout<<b;
 }
 else
 {
  while(ctr<=n)
  {
   fib=a+b;
   a=b;
   b=fib;
   ctr++;
   cout<<fib<<" ";
  }
 }
}