Examples on different ways to calculate greatest common divisor (gcd) of two integers (vlaue of both positive and negative integers ) using loops and decision making statements in C programing.

## C Program to Find GCD of two Numbers

Contents

The HCF (highest common factor) or GCD (greatest common divisor) of two integers is the largest integer that can exactly divide both numbers (without a remainder).

hear we will show you 3 different way to find gcd program in c with greatest common divisor (gcd).

Simple calculate and method of GCD

**gcd of two numbers in c**

82 = 2^{1} 41^{1}

152 = 2^{3} 19^{1}

The answer is: 2^{1} = 2

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

// include header file #include <stdio.h> // main function int main() { int num1, num2, i, gcd; // insert data printf("Enter two integers value : "); scanf("%d %d", &num1, &num2); // calculate gcd for(i=1; i <= num1 && i <= num2; ++i) { // Checks if i is factor of both integers if(num1%i==0 && num2%i==0) gcd = i; } // print result printf("\nGCD of %d and %d = %d", num1, num2, gcd); // return 0 return 0; }

#### Output

Enter two integers value : 82 152 GCD of 82 and 152 = 2

In this program, two integers entered by the user are stored in variable num1 and num2. loop is iterated until βiβ is less than βnum1β and βnum2β.

In each iteration, if both βnum1β and βnum2β are exactly divisible by βiβ, the value of βiβ is assigned to gcd.

When the for loop is completed, the greatest common divisor(gcd) of two numbers is assigned in variable βgcdβ.

### Example 2 :: GCD Using while loop and if-else Statement

// include header file #include <stdio.h> // main function int main() { int num1, num2; // insert data printf("Enter two positive integers value : "); scanf("%d %d",&num1,&num2); // calculate gcd while(num1!=num2) { if(num1 > num2) num1 -= num2; else num2 -= num1; } // print result printf("\nGCD IS = %d",num1); // return 0 return 0; }

#### Output

Enter two positive integers value : 82 152 GCD IS = 2

This is a better way to find the greatest common divisor(GCD). In this method, smaller integer is subtracted from the larger integer, and the result is assigned to variable who is holding larger integer value. This process is continued until βnum1β and βnum2β are equal.

The above two programs works as intended only if the user pass positive integers(value). Here is a little change of the second example to find the GCD for both positive and negative type of integers.

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

// include header file #include <stdio.h> // main function int main() { int num1, num2; // insert data printf("Enter two integers value : "); scanf("%d %d",&num1,&num2); // if user enters negative number, // then sign of the number will be changed to positive num1 = ( num1 > 0) ? num1 : -num1; num2 = ( num2 > 0) ? num2 : -num2; // calculate gcd while(num1!=num2) { if(num1 > num2) num1 -= num2; else num2 -= num1; } // print result printf("\nGCD IS = %d",num1); // return 0 return 0; }

#### Output

Enter two integers value : 82 -152 GCD IS = 2

I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I’m a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.