diff --git a/CPP/GCD.cpp b/CPP/GCD.cpp new file mode 100644 index 000000000..ef575e1c5 --- /dev/null +++ b/CPP/GCD.cpp @@ -0,0 +1,25 @@ +#include +using namespace std; + +int main() { + int n1, n2, hcf; + cout << "Enter two numbers: "; + cin >> n1 >> n2; + + // swapping variables n1 and n2 if n2 is greater than n1. + if ( n2 > n1) { + int temp = n2; + n2 = n1; + n1 = temp; + } + + for (int i = 1; i <= n2; ++i) { + if (n1 % i == 0 && n2 % i ==0) { + hcf = i; + } + } + + cout << "HCF = " << hcf; + + return 0; +}