Highest Common Factor(HCF) of two numbers in C

In this example, we will learn to find the HCF in c program of two numbers using different loops and conditions. 

Below I shared source code in user have to enter two numbers and for loop checks conditions. Can both values be less or equal to the values of I's? if yes, then if the condition checks for a%i==0&&b%i==0 and condition true then the value of i is highest common factor of two numbers.


HCF Program in C


#include<stdio.h>
int main(){
int hcf,i,a,b;
scanf("%d%d",&a,&b);
        printf("a = %d",a);
        printf("b = %d",b);
for(i=1;i<=a&&i<=b;i++)
{
if(a%i==0&&b%i==0)
{
hcf=i;
}
}
printf("HCF = %d",hcf);
return 0;
}

Output


a = 12
b = 20
HCF = 4
--------------------------------
In the above article, we discuss HCF in c program and don't forget to share your experience in the comment box.

Happy Coding 😊

No comments:

If you have any doubts, Please let me know

Powered by Blogger.