Tuesday, February 13, 2024

13. WAP to calculate GCD of 2 number (i) with recursion (ii) without recursion

13. WAP to calculate GCD of 2 number (i) with recursion (ii) without recursion


PROGRAME CODE

#include<stdio.h>

#include<iostream>

using namespace std;

class gcd{

            public:

                        int a,b,k;

                        gcd(){

                                    a=0;

                                    b=0;

                                    k=0;

                        }

                        void calculate(){

                                    if(a==0||b==0)

                                                cout<<"THERE IS A ERROR ......"<<endl;

                                    else if(a>b){

                                                int key=a;

                                                a=b;

                                                b=key;

                                    }

                                    while(a!=0){

                                                int c=b/a;

                                                int d=b%a;

                                                if(d==0)

                                                            k=a;                                        

                                                b=a;

                                                a=d;

                                    }

                        }

                        int calculate_rec(int a,int b){

                                    if(a==0 && b==0)

                                                cout<<"THERE IS A ERROR ......"<<endl;

                                    else if(a>b){

                                                int key=a;

                                                a=b;

                                                b=key;

                                    }

                                    int c=b/a;

                                    int d=b%a;

                                    if(d==0)

                                                return a;                                            

                                    b=a;

                                    a=d;

                                    calculate_rec(a,b);                          

                        }

                        void display(){

                                    if(a==0 && b==0)

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

                                    cout<<"THE GCD IS -> "<<k<<endl;

                        }

};

int main(){

            gcd ob;

            int opt=0;

            do{

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

                        cout<<"PRESS 1 TO CALCULATE GCD.........."<<endl;

                        cout<<"PRESS 2 TO CALCULATE GCD USING RECURTION......."<<endl;

                        cout<<"PRESS 3 TO DISPLAY..."<<endl;

                        cin>>opt;

                        switch(opt){

                                    case 0:

                                                break;

                                    case 1:

                                                cout<<"ENTER THE 1st NUMBER ->"<<endl;

                                                cin>>ob.a;

                                                cout<<"ENTER THE 2nd NUMBER ->"<<endl;

                                                cin>>ob.b;

                                                ob.calculate();

                                                break;

                                    case 2:

                                                cout<<"ENTER THE 1st NUMBER ->"<<endl;

                                                cin>>ob.a;

                                                cout<<"ENTER THE 2nd NUMBER ->"<<endl;

                                                cin>>ob.b;

                                                cout<<"THE GCD IS -> "<<ob.calculate_rec(ob.a,ob.b)<<endl;

                                                break;                                    

                                    case 3:

                                                ob.display();

                                                break;

                                    default:

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

                        }

                       

            }while(opt!=0);

            return 0;

}


No comments:

Post a Comment