1,2,3,4,6 are factors of number 12

We will ask user to enter two input numbers and then find out the factors of both which are common to both

```
#include <stdio.h>
int main(void){
int i;
int num1,num2,highest_number,highest_common=0;
printf("Enter the first number ");
scanf("%d",&num1);
printf("Enter the second number ");
scanf("%d",&num2);
if(num1>num2){
highest_number=num1;
}else{highest_number=num2;}
for ( i=1;i<highest_number;i++){
if(num1 % i ==0 && num2 % i==0){
printf("Common factor = %d \n",i);
highest_common=i; // common factor is passed
}
}
printf("Highest common factor %d",highest_common);
return 0;
}
```

Output is here
```
Enter the first number 12
Enter the second number 18
Common factor = 1
Common factor = 2
Common factor = 3
Common factor = 6
Highest common factor 6
```

The greatest common divisor (GCD) of two or more numbers is the greatest common factor number that divides them, exactly. It is also called the highest common factor (HCF).

Enhance the tutorial by allowing the user to input two numbers for calculating the GCD.

```
#include <stdio.h>
int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
int main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
printf("GCD of %d and %d is %d\n", num1, num2, gcd(num1, num2));
return 0;
}
```

```
Enter two numbers: 56 98
GCD of 56 and 98 is 14
```

This example demonstrates how to calculate the GCD using a recursive function.

```
#include <stdio.h>
int gcd_recursive(int a, int b) {
if (b == 0)
return a;
return gcd_recursive(b, a % b);
}
int main() {
int num1 = 48, num2 = 18;
printf("GCD of %d and %d is %d\n", num1, num2, gcd_recursive(num1, num2));
return 0;
}
```

```
GCD of 48 and 18 is 6
```

This example calculates the GCD of more than two numbers using an iterative approach.

```
#include <stdio.h>
int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
int gcd_multiple(int arr[], int n) {
int result = arr[0];
for (int i = 1; i < n; i++) {
result = gcd(result, arr[i]);
}
return result;
}
int main() {
int numbers[] = {120, 180, 240};
int n = sizeof(numbers) / sizeof(numbers[0]);
printf("GCD of multiple numbers is %d\n", gcd_multiple(numbers, n));
return 0;
}
```

```
GCD of multiple numbers is 60
```

Common factor of two Numbers

This article is written by **plus2net.com** team.