Make a program of hcf of two numbers in c programming
Certainly! Below is a simple C program to calculate the Highest Common Factor (HCF) of two numbers using the Euclidean algorithm.
#include <stdio.h>
// Function to calculate HCF using the Euclidean algorithm
int calculateHCF(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
int main() {
int num1, num2, hcf;
// Input two numbers from the user
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
// Calculate HCF
hcf = calculateHCF(num1, num2);
// Output the result
printf("The HCF of %d and %d is: %d\n", num1, num2, hcf);
return 0;
}
Function calculateHCF(int a, int b)
: This function implements the Euclidean algorithm to find the HCF. It continues to replace a
with b
and b
with a % b
until b
becomes zero. At that point, a
contains the HCF.
main()
function:
calculateHCF
function to compute the HCF of the two numbers.hcf.c
.gcc hcf.c -o hcf
./hcf
You can then enter two numbers, and the program will output their HCF.