A program to display the following series (i.e. Fibnocci series):

        3/1, 8/4, 17/9, 34/18, 67/35, ......, x/y

where the user inputs either x or y.

 

#include<iostream.h>
#include<math.h>
#include<conio.h>

void main()
{
clrscr();
int c;
double n=3,d=1,a=5,b=2,x,y;

cout<<"1. Enter limit of numerator."<<endl;
cout<<"2. Enter limit of denominator."<<endl;
cout<<"Enter your choice: ";
cin>>c;

switch(c)
 {
  case 1: {
              cout<<"\nEnter limit of numerator: ";
              cin>>x;
              cout<<endl;
 
             if(x>=3)
              cout<<n<<"/"<<d<<" ";

              d=d+n;
              n=n+a;
 
              if(x>=8)
              cout<<n<<"/"<<d<<" ";
 
              do
               {
                d=d+a;    
                a=a+pow(2,b);
                n=n+a;
                cout<<n<<"/"<<d<<" ";
                b++;
               }
              while(n<x);

              break;
             }
 case 2: {
             cout<<"\nEnter limit of denominator: ";
             cin>>y;
             cout<<endl;
 
             if(y>=1)
              cout<<n<<"/"<<d<<" ";
 
             d=d+n;
             n=n+a;
 
             if(y>=4)
              cout<<n<<"/"<<d<<" ";
 
             do
              {
               d=d+a;
               a=a+pow(2,b);
               n=n+a;
               cout<<n<<"/"<<d<<" ";
               b++;
              }
             while(d<y);
 
             break;
            }
 default: {
             cout<<"\nWrong choice entered.\n";
             break;
            }
   } 
  getch();
}

 

< Back