C++ Program to calculate the Highest Common Factor


The highest Common Factor or Greatest Common Divisor are factors that are maximum and that can divide two or more values without generating any remainder. In this article, we shall discuss a few methods to perform HCF / GCD between two numbers in C++.

This is simply a mathematical solution and there are several algorithms present to find GCD. The Euclidean method to find GCD is common. The same algorithm we will use in iterative mode, and recursive mode.

Using Iterative method

The iterative solution for the Euclidean gcd finding algorithm is shown in the algorithm section.

Algorithm

  • Take two numbers a and b as input.
  • If a is 0, then return b.
  • If b is 0, return a.
  • While a and b are not the same, do.
    • If a > b, then a := a – b.
    • Otherwise b := b - a.
  • End While.
  • Return HCF as variable a.

Example

#include <iostream> using namespace std; int solve( int x, int y) { if (x == 0) return y; else if (y == 0) return x; while (x != y) { if (x > y) x = x - y; else y = y - x; } return x; } int main( int argc, char* argv[] ) { cout << "HCF of two numbers 12 and 72 is: " << solve(12, 72) << endl; cout << "HCF of two numbers 18 and 13 is: " << solve(18, 13) << endl; cout << "HCF of two numbers 117 and 96 is: " << solve(117, 96) << endl; cout << "HCF of two numbers 85 and 15 is: " << solve(85, 15) << endl; }

Output

HCF of two numbers 12 and 72 is: 12
HCF of two numbers 18 and 13 is: 1
HCF of two numbers 117 and 96 is: 3
HCF of two numbers 85 and 15 is: 5

Using Iterative method

The same Euclidean method can be implemented using the recursive method. Here we are describing the recursive method definition in the following algorithm.

Algorithm

  • Define a function HCF that takes two numbers a and b.
  • If a is 0, then return b
  • Otherwise return HCF( b mod a, a)

Example

#include <iostream> using namespace std; int solve( int x, int y) { if (x == 0) return y; return solve( y % x, x ); } int main( int argc, char* argv[] ) { cout << "HCF of two numbers 12 and 72 is: " << solve(12, 72) << endl; cout << "HCF of two numbers 18 and 13 is: " << solve(18, 13) << endl; cout << "HCF of two numbers 117 and 96 is: " << solve(117, 96) << endl; cout << "HCF of two numbers 85 and 15 is: " << solve(85, 15) << endl; }

Output

HCF of two numbers 12 and 72 is: 12
HCF of two numbers 18 and 13 is: 1
HCF of two numbers 117 and 96 is: 3
HCF of two numbers 85 and 15 is: 5

Conclusion

Getting Highest Common Factor or Greatest Common Divisor is a very useful thing while solving different mathematical problems. The Euclidean method can be used to calculate the same. The same method can be applied iteratively as well as recursively. Here we have shown both of them. On the other hand, we can calculate GCD/HCF with the Least Common Multiplier (LCM). The GCD and LCM of two numbers are the same as the multiplication of these two numbers. So from that principle, if we know the LCM and the multiplication of these two numbers, the GCD can be calculated easily.

Updated on: 04-Apr-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements