Tuesday, February 13, 2024

12. (ii) WAP to display Fibonacci series (i)using recursion, (ii) using iteration

 12. (ii) WAP to display Fibonacci series (i)using recursion, (ii) using iteration


PROGRAME CODE

#include<stdio.h>

#include<iostream>

using namespace std;

class fibo{

                public:

                                void fibo1(int num){

                                                int a=1,b=1,c=0;

                                                for(int i=1;i<=num;i++){

                                                                if(i==1)

                                                                                cout<<a<<" ";

                                                                else if(i==2)

                                                                                cout<<b<<" ";

                                                                else{

                                                                                c=a+b;

                                                                                a=b;

                                                                                b=c;

                                                                                cout<<c<<" ";                                   

                                                                }

                                                }

                                }

                                int fib(int num){

                                                if(num==1||num==2)

                                                                return 1;

                                                return(fib(num-1)+fib(num-2));

                                }                             

                                void fibo2(int num){

                                                for(int i=1;i<=num;i++){

                                                                cout<<fib(i)<<" ";

                                                }

 

                                }

};

int main(){

                fibo ob;

                int num=0,opt=0;

                do{

                                cout<<"\nPRESS 0 TO EXIT ..."<<endl;

                                cout<<"PRESS 1 TO CALCULATE FIBONACCI SERIES ...."<<endl;

                                cout<<"PRESS 2 TO CALCULATE FIBONACCI SERIES USING RECERSION..........."<<endl;

                                cin>>opt;

                                switch(opt){

                                                case 0:

                                                                break;

                                                case 1:

                                                                cout<<"ENTER THE POSITION OF SEREIES....."<<endl;

                                                                cin>>num;

                                                                ob.fibo1(num);

                                                                break;

                                                case 2:

                                                                cout<<"ENTER THE POSITION OF SEREIES....."<<endl;

                                                                cin>>num;

                                                                ob.fibo2(num);

                                                                break;                                                  

                                                default:

                                                                cout<<"...........ERROR............"<<endl;

                                }

                }while(opt!=0);

                return 0;

}


No comments:

Post a Comment