In this video, we will see a C++ program to find the GCD of two numbers.
Basically, GCD (Greatest Common Divisor) or HCF (Highest Common Factor) of two numbers is the largest number that divides both of them.
For examples:
Input: GCD of 98 and 56 is 14
Num 1 = “98”
Num 2 = “56”
Then 98 = 2x7x7
56= 2x2x2x7
Common factor = 2x7
GCD = 14
To find GCD of two numbers we use three different methods:
1) Using loop
2) Using recursive function
3) Using In-built [ __GCD()] function
Apart from that, we will see the time and space complexity of each and every method.
Here,
- Time and space complexity will be O(N) & O(1) in looping method.
- Using recursive function time and space complexity will be O(log(N)) & O(1) respectively.
- And Using In-built [ __GCD()] function the time and space complexity will be O(log(N)) & O(1).
Program to find GCD of two numbers: https://www.geeksforgeeks.org/c-program-find-gcd-hcf-two-numbers/