Wednesday, 18 December 2013

C++ PROGRAM TO FIND THE G.C.D OF TWO NUMBERS USING NON-RECURSIVE FUNCTIONS

/* C++ PROGRAM TO FIND THE G.C.D OF TWO NUMBERS USING NON-RECURSIVE FUNCTIONS */
#include<iostream.h>
#include<conio.h>
int gcd(int m,int n);
void main()
{
    int m,n;
    clrscr();
    cout<<"Enter m,n values :";
    cin>>m>>n;
    cout<<"The G.C.D of given two numbers :"<<gcd(m,n);
    getch();
}
int gcd(int m,int n)
{
    if(m==n)
        return m;
    else
    {
        int gcd;
        while(n!=0)
        {
            int d=m%n;
            if(d==0)
                gcd=n;
            m=n;
            n=d;
        }
        return gcd;
    }
}

/*   OUTPUT
     ------
     Enter m,n values :6 8
     The G.C.D of given two numbers :2
*/

No comments:

Post a Comment