# Find gcd of two numbers in c

GCD (Greatest Common Divisor) or HCF (Highest Common Factor) of two numbers is the largest number that divides both of them.
Examples on different ways to calculate GCD of two integers (for both positive and negative integers)

## Example #1: GCD Using for loop and if Statement

```#include <stdio.h>
#include<conio.h>
void main()
{
int num1, num2, j, gcd;
printf("\n\t\t\t...Welcome To EduNews.Tech... ");

printf("\n\nEnter two integers: ");
scanf("%d %d", &num1, &num2);

for(j=1; j <= num1 && j <= num2; ++j)
{
// Checks if i is factor of both integers
if(num1%j==0 && num2%j==0)
gcd = j;
}
printf("G.C.D of %d and %d is %d", num1, num2, gcd);
printf("\n\n\n\t\t\tThankyou for Joining Us !");
printf("\n\t\t\t!Regards EduNews !");

getch();
}
```

## Example #2: GCD for both positive and negative numbers

```#include <stdio.h>
#include<conio.h>
void main()
{
int num1, num2;
printf("\n\t\t\t...Welcome To EduNews.Tech... ");

printf("\n\nEnter two integers: ");
scanf("%d %d",&num1,&num2);

// if user enters negative number, sign of the number is changed to positive
num1 = ( num1 > 0) ? num1 : -num1;
num2 = ( num2 > 0) ? num2 : -num2;

while(num1!=num2)
{
if(num1 > num2)
num1 -= num2;
else
num2 -= num1;
}
printf("GCD = %d",num1);
printf("\n\n\n\t\t\tThankyou for Joining Us !");
printf("\n\t\t\t!Regards EduNews !");

getch();
}
```

### Program Output:

I hope this post helps you to understand the “GCD Program” and its implementation in C programming language.

Keep coding ðŸ™‚