on Leave a Comment

C++ Program to Find GCD

GCD stands for Greatest Common Divisor.

GCD or HCF of two integer is greatest common number that can divide both integer.

C++ Program to Find GCD

#include <iostream>
using namespace std;
int main()
{
    int a,b,g;
    cout<<"Enter two numbers ";
    cin>>a>>b;
    for(g=a>b?a:b; g>=1; g--)
        if(a%g==0 && b%g==0)
            break;
    cout<<"\nGreatest Common Factor(GCD) of "<<a<<" and "<<b<<" is "<<g;
    return 0;
}

OUTPUT 1:

Enter two numbers 69
72

Greatest Common Factor(GCD) of 69 and 72 is 3


OUTPUT 2:

Enter two numbers 15
20

Greatest Common Factor(GCD) of 15 and 20 is 5

See also:




0 comments:

Post a Comment