Write a C program enter two numbers and find the HCF(Highest Common Factor) using for loop. How to find GCD(HCF) of any two given numbers using loops in C programming. C program to print GCD(HCF).

Happy coding ;)

**Example:**HCF of 12 and 30 is 6### Required knowledge

Basic C programming, If else, Conditional operator, For loop#### HCF(Highest Common Factor)

HCF is the greatest number that divides exactly two or more numbers. HCF is also known as GCD(Greatest Common Divisor) or GCF(Greatest Common Factor).**Example:**### Program to find GCD (HCF)

/** * C program to find HCF(Highest Common Factor) of two numbers */ #include <stdio.h> int main() { int i, num1, num2, min, hcf=1; /* * Reads two numbers from user */ printf("Enter any two numbers to find HCF: "); scanf("%d %d", &num1, &num2); min = (num1<num2) ? num1 : num2; for(i=1; i<=min; i++) { /* * If i is factor of both number */ if(num1%i==0 && num2%i==0) { hcf = i; } } printf("HCF of %d and %d = %d\n", num1, num2, hcf); return 0; }

Also don't forget to check this program using recursive approach - C program to find GCD (HCF) of any two numbers.

Output

Enter any two numbers to find HCF: 12

30

HCF of 12 and 30 = 6

30

HCF of 12 and 30 = 6

Happy coding ;)

### You may also like

- Loop programming exercises index.
- C program to find LCM of two numbers.
- C program to check whether a number is divisible by 5 and 11 or not.
- C program to find maximum between two using conditional operator.
- C program to print all factors of a number.
- C program to print all Prime Factors of a number.
- C program to calculate Factorial of any number.
- Write a C program to print Fibonacci series up to n terms.
- C program to print table of any number in proper format.
- C program to find reverse of a number.
- C program to enter P, T, R and calculate Simple Interest.
- C program to enter P, T, R and calculate Compound Interest.
- C program to check whether a number is Prime number or not.
- C program to check whether a number is Armstrong number or not.
- C program to check whether a number is Strong number or not.
- C program to check whether a number is Perfect number or not.

< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).