In this tutorial, we are going to learn a writing program in C to calculate GCD or HCF. Basically, Both GCD and HCF are the same. GCD full form is Greatest common divisor and HCF means Highest Common Divisor.
Before starting the writing a program in C for HCF or GCD let’s understand HCF/GCD first.
What is GCD/HCF? How to calculate it?
GCD stands for greatest common divisor (GCD), It is a largest non-zero positive integer of two or more integers that divides each of the integers.
GCD is same as HCF. It can be justify like “greatest common divisor”, the adjective “greatest” may be replaced by “highest”, and the word “divisor” may be replaced by “factor”, so that other name of GCD can be HCF.
How to calculate GCD/HCF?
To calculate GCD of any two or more number, first we will find the factors of the numbers. After that will select the common number and multipy them, resultant will be GCD.
Let’s see an example
Suppose we need to calculate the GCD of 26 and 38.
Factors of 26 = 2 * 13
Factors of 38 = 2 * 19
Here 2 is only the common. So GCD(26,38) will be 2 because only 2 is the highest number that can divide both 26 and 38.
C Program to calculate GCD of two numbers
#include <stdio.h>
int main()
{
int num1, num2, i, gcd;
printf("Program to find HCF or GCD of two numbers\n");
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
for(i=1; i <= num1 && i <= num2; ++i)
{
if(num1%i==0 && num2%i==0)
gcd = i;
}
printf("G.C.D of number %d and %d is %d", num1, num2, gcd);
return 0;
}
Output
Program to find HCF or GCD of two numbers
Enter the first number: 34
Enter the second number: 12
G.C.D of number 34 and 12 is 2
Also Prepare Below Important Question
- Hibernate Interview Questions for 2+ years of experience
- 68 Most Important Microservices Interview Questions
- 60 Most Important Git Interview Questions
- 50+ Mostly asked Java Interview Questions for 6 Years Exp.
- 60+ Mostly Asked Java Interview Questions for 4 Years Exp
- 60+ Mostly asked Java Interview Question for 3+ Years
- 60+ Spring Boot interview questions for 4+ years Exp.
- 60+ Mostly Asked Spring Boot Interview Questions for 3+ Yrs
- Scenario Based Java 8 Coding Interview Questions (For Experienced)
- Python Program to add two numbers without addition operator
- Mostly Asked Java Interview Questions For 2 Yrs Experience
- Find All Pairs in Array whose Sum is Equal to given number Using Java
- Java Program to find GCD of two Numbers using Recursion
- Python Program to Separate Characters in a Given String
- Python Program to add two number using Recursion
- Python Program to Find Highest Frequency Element in Array
- Python Program to Merge two Arrays
- Perform left rotation by two positions in Array Using Python
- Python Program to Delete Element at Given Index in Array
- Python Program to Delete element at End of Array
Interview Questions Categories
C Programming Interview Preparation
Core Java Programming Interview Preparation
- Core Java Programming Coding Questions
- Core Java Pattern Programming Questions
- Core Java Programming Interview Questions