# Program for HCF

## HCF

We will write a C program for calculate the HCF  and the number will be entered by the user. This will help to understand the basic structure of programming. In this program , we will display the HCF of given numbers easily by using proper syntax and algorithms. ## Working of Program :

In the program, we will require some numbers from the user to display the HCF of given numbers.

### Important Note :

• HCF is highest common factor, it is used for two or more number to obtain the greatest number which divides all given numbers.

• HCF is the greatest factor of given two numbers.

### Syntax of for() Loop:-

```for (Initialization, condition, updation)
{
//code
}```

### Problem 1

Write a program to calculate the HCF of given numbers using for loop.

• Firstly, we have to enter the number.
• Then print the HCF of given numbers.

### Code

Run
```#include<stdio.h>
int main ()
{
int i, num1, num2, min, hcf = 1;
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 (num1 % i == 0 && num2 % i == 0)
{
hcf = i;
}
}
printf ("HCF of %d and %d = %d\n", num1, num2, hcf);
return 0;
}
```

### Output

```Enter any two numbers to find HCF = 4555
7889
HCF of 4555 and 7889 = 1
```

### Note:

In the following program we will display the HCF of given numbers.

```Problem 2
```

Write a program to calculate the  HCF  of given numbers.

• Firstly, we have to enter the number.
• Then print the HCF.

### Code

Run
```#include<stdio.h>
int main()
{
int a,b,rem;
printf("Enter any two numbers = ");
scanf("%d%d",&a,&b);
while(b!=0)
{
rem=a % b;
a=b;
b=rem;
}
printf("HCF of two numbers is = %d",a);
return 0;
}```

### Output

```Enter any two numbers = 54
67
HCF of two numbers is = 1
```

### Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

## Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others