# program to find gcd or hcf of two numbers c with code examples

GCD or HCF stands for Greatest Common Divisor or Highest Common Factor, and it is an extremely important mathematical concept. It is the largest number that divides two or more numbers without leaving any remainder.

In this article, we will discuss an efficient approach to finding GCD or HCF of two numbers. We will also provide code examples to help you understand the concept better.

Algorithm to find GCD or HCF of two numbers:

There are several approaches to find the GCD or HCF of two numbers, but the most common algorithm is Euclid's algorithm. The algorithm is based on the observation that the GCD of two numbers also divides their difference. The algorithm is as follows:

1. Input the two numbers a and b.

2. If b is zero, then the GCD or HCF of a and b is a. Return a.

3. Otherwise, compute the remainder of a divided by b.

4. Recursively call the algorithm with b as the first argument and the remainder as the second argument.

Example:

Now let’s say we have two numbers 24 and 36, and we want to find their GCD or HCF. Here’s how we can implement Euclid’s algorithm to find it:

Step 1: a = 24, b = 36.

Step 2: Since b is not zero, we compute the remainder of a divided by b:

24 % 36 = 24.

Step 3: We recursively call the algorithm with b as the first argument and the remainder as the second argument:

GCD(36, 24)

Step 4: Since b is not zero, we compute the remainder of 36 divided by 24:

36 % 24 = 12.

Step 5: We recursively call the algorithm with b as the first argument and the remainder as the second argument:

GCD(24, 12)

Step 6: Since b is not zero, we compute the remainder of 24 divided by 12:

24 % 12 = 0.

Step 7: Since the remainder is zero, we return the value of b, which is 12. Hence, the GCD or HCF of 24 and 36 is 12.

Code Example:

Here is a code example in Python to find the GCD or HCF of two numbers using Euclid’s algorithm:

```def gcd(a, b):
if b == 0:
return a
else:
return gcd(b, a % b)

a = 24
b = 36
print("GCD of", a, "and", b, "is", gcd(a, b))
```

Output: GCD of 24 and 36 is 12.

Similarly, you can write code to find the GCD or HCF of two numbers in any programming language. The algorithm remains the same, only the syntax will differ depending on the programming language you’re using.

Conclusion:

In conclusion, finding the GCD or HCF of two numbers is an important concept in mathematics. This is why Euclid’s algorithm is widely used to compute it. With the help of code examples, we hope we have made the concept clear to you.

here are more details about the topics mentioned earlier:

1. Binary Search:

Binary search is a searching algorithm that works by dividing the list of elements into two parts and then recursively searching in the half where the target element might possibly be present. It is an efficient algorithm that can be used for searching in sorted arrays or lists. The time complexity of binary search is O(log n) in the worst case, where n is the number of elements in the list.

1. Bubble Sort:

Bubble sort is a simple sorting algorithm that works by repeatedly swapping adjacent elements if they are in the wrong order. It is an inefficient algorithm for large lists but can be useful for small lists. The time complexity of bubble sort is O(n^2) in the worst case, where n is the number of elements in the list.

1. Quick Sort:

Quick sort is a divide and conquer algorithm that works by selecting a pivot element and partitioning the list such that all elements smaller than the pivot are placed before it, and all elements greater than the pivot are placed after it. The process is repeated recursively on each partition until the list is sorted. It is an efficient sorting algorithm with an average time complexity of O(n log n), but the worst-case time complexity can be O(n^2), depending on the choice of pivot.

1. Merge Sort:

Merge sort is also a divide and conquer algorithm that works by recursively dividing the list into two halves, sorting each half, and then merging the two sorted halves. It is an efficient algorithm with a time complexity of O(n log n) in the worst case, where n is the number of elements in the list.

1. GCD or HCF of two numbers:

As mentioned earlier, GCD or HCF stands for Greatest Common Divisor or Highest Common Factor, and it is the largest number that divides two or more numbers without leaving any remainder. Euclid's algorithm is the most common approach used to find the GCD or HCF of two numbers, and it is based on the observation that the GCD of two numbers also divides their difference. The algorithm has a time complexity of O(log n), where n is the maximum of the two numbers.

## Popular questions

1. What is the full form of GCD or HCF?
Answer: GCD stands for Greatest Common Divisor, and HCF stands for Highest Common Factor. They refer to the same concept in mathematics.

2. Which algorithm is commonly used to find the GCD or HCF of two numbers?
Answer: Euclid's algorithm is the most commonly used algorithm to find the GCD or HCF of two numbers.

3. What is the time complexity of Euclid's algorithm for finding GCD or HCF?
Answer: The time complexity of Euclid's algorithm for finding GCD or HCF is O(log n), where n is the larger of the two numbers.

4. How does Euclid's algorithm work?
Answer: Euclid's algorithm works by repeatedly finding the remainder of the larger number divided by the smaller number until the remainder becomes zero. The last non-zero remainder is the GCD or HCF of the two numbers.

5. Can you provide an example code for finding GCD or HCF using Euclid's algorithm?
Answer: Sure, here is an example code in Python:

```def gcd(a, b):
if b == 0:
return a
else:
return gcd(b, a % b)

# Example Usage:
a = 24
b = 36
result = gcd(a, b)
print("GCD of", a, "and", b, "is", result)
```

Output: GCD of 24 and 36 is 12.

### Tag

Euclidean ##### Sricharan S
As an experienced software engineer, I have a strong background in the financial services industry. Throughout my career, I have honed my skills in a variety of areas, including public speaking, HTML, JavaScript, leadership, and React.js. My passion for software engineering stems from a desire to create innovative solutions that make a positive impact on the world. I hold a Bachelor of Technology in IT from Sri Ramakrishna Engineering College, which has provided me with a solid foundation in software engineering principles and practices. I am constantly seeking to expand my knowledge and stay up-to-date with the latest technologies in the field. In addition to my technical skills, I am a skilled public speaker and have a talent for presenting complex ideas in a clear and engaging manner. I believe that effective communication is essential to successful software engineering, and I strive to maintain open lines of communication with my team and clients.
Posts created 3085