×

GCD of Two Numbers in C

  • The GCD means the greatest common divisor of two or more integers, it is also called as hcf. The gcd returns the greatest integer of the given two integers that divides the numbers without leaving any remainder.
  • For example, for the two numbers 5 and 10, both 5 and 10 are divisible by 1 and 5. Since 5 is greater than 1, the gcd of 5 and 10 is 5.
  • We can find the gcd of two numbers using loops and statements.
    Program 1: GCD of two numbers using if statement and for loop

/ / program to find out gcd of two numbers
#include <stdio.h>
/ / main function
Int main () 
{
/ / declaring the variable
Int a, b, i, gcd;
Printf(“ Enter the two numbers : “);
Scanf(“%d %d “,&a,&b);
for(Ii= 1; i <= a && i <= b;i++)
{
/ / checks whether i is a factor for two integers
If( a% i == 0 && b % i == 0)
{
gcd = i;
} }
Printf(“ The gcd of two numbers is %d” , gcd);
return 0;
}

Output:

Enter two numbers: 8

12

The gcd of two numbers is 4

  • In the above program, the for loop is used to block the i value to not to check beyond the user-specified values

    The gcd of two numbers using while loop:
/ / gcd of two numbers using while loop
#include <stdio.h>
/ / main function
Int main () {
/ / declaring the variable
Int a, b, gcd, temp;
Printf(“ Enter the two numbers : “);
Scanf(“%d %d “,&a,&b);
/ / using while loop
while (b != 0) {
temp = b;
b = a % b;
a = temp:
}
Gcd = a;
Printf(“ The gcd of two numbers is %d ”, gcd);
return 0;
}

Output: Enter the two numbers: 8 12

The gcd of two numbers is 4

  • In the above program, in the while loop, we are checking whether the b is equal to zero or not, If the value is equal to zero, the loop ends. Else the statements in the loop are executed.
    The gcd of two numbers using recursion:
  • This program is used to find the gcd of two numbers by recursively calling the function.
/ / program to find out gcd by using recursion
# include <stdio.h>
Int gcd (int a,int b);
/ / main function
Int main () {
Printf(“ Enter the two numbers : “);
Scanf(“%d %d “,&a,&b);


printf(“ The gcd of two numbers is %d ”, gcd(a,b));
return 0;
}
Int gcd (int a, int b) {
If (b == 0) {
Return a;
}
else {
return gcd (b,a%b);
} }

Output:

Enter the two numbers: 81

 153

The gcd of two numbers is 9

  • In the above program, in the main function, the line gcd (int a,int b) makes the compiler go to that function.
  • In the function, the line gcd ( b, a% b) line is used to call the function recursively.
  • And it is essential in c language in recursive program to write a base case, this base case act as a condition and checks every time not to execute the loop infinite times.
  • Here (b==0) is the base condition. If the value reaches this state, then the loop execution stops.
    Gcd of positive and negative integers:
/ * Program to find gcd of two numbers for positive and negative integers * /
#include <stdio.h>
/ / main function
Int main () {
Int a,b;
Printf(“ Enter the two numbers : “);
Scanf(“%d %d “,&a,&b);
/ / if any of the integers is negative, then the sign of integer is changed
a = ( a > 0) ? a : -a;
b = ( b > 0) ? b : -b;
while (a != b);{
If (a > b)
a= a – b;
else
b = b – a;}
Printf(“ The gcd of two numbers is %d ”, gcd);
return 0;
}


Output: Enter the two numbers: 81

- 153

The gcd of two numbers is 9

  • In the above program, if any of the input integers is negative, it is changed to a positive integer by changing the sign of the integer.

Related Topics

Type Conversion in the C

The process of changing one data type to another in the C programming language is known as "type conversion." The type conversion method is only executed on data types that...

4 minutes read.

Entry Control Loop in C

Initially, an entry control loop verifies the termination condition at the entry point. After that, its control passes to the main body of the while or for loop if the...

3 minutes read.

Pi() Function in C

Pi: Mathematic constants are not defined in the c or c++ programming languages. To use the Mathematical constants firstly, we have to define the _USE_MATH_DEFINES and then declare the cmath and...

3 minutes read.

Macros in C

In the C programming language, the macro is defined as a segment of the code replaced by the macro defined value at the beginning. ‘#define’ is the directive that defines...

3 minutes read.

Pre-increment and Post-increment in C

The rich set of operators is supported by the c language. In c there are several operators used. The mathematical operations in the programming language are done with the operators...

4 minutes read.

memmove() in C

Introduction: In this article we are discuss about the memmove() function in C. This function transfers memory blocks from one location to another. The memmove() function is declared in the...

3 minutes read.

String Handling functions in C

String :- The String is the collection of characters. Every String ends with the null character, and the String is enclosed in the double quotations .ie, "javaTpoint". If we see any...

5 minutes read.

Multiplication Table in C

Displaying table up to 10 #include <stdio.h> int main() {     int num, i = 1;     printf("     Enter any Number:");     scanf("%d", &num);     printf("Multiplication table of %d: ", num);  ...

1 minute read.

Ferror() in c

Ferror():  In C, the ferror() function checks assuming there is a blunder in the given stream. The ferror() function is utilized to check for the document blunder on given stream. A return...

4 minutes read.

Type qualifiers in C

Type qualifiers in C: In the C programming language, type qualifiers are the keywords that prepend to the variables to change their accessibility, i.e., we can tell that the type...

4 minutes read.

Do-While Loop in C

Both 'While' and 'Do-While' loops in C mostly have a similar concept, and the code of both loops runs for mainly similar purposes in the same manner. But, there is...

3 minutes read.

Bitwise XOR Operator in C

What is Bitwise? As we know, bits are the smallest unit of a number and are mostly used in a computing system. So, in bitwise, we do operations on bits instead...

3 minutes read.

File Operations in C

Why do I need the file? All data will be lost when the program exits. Saving data to a file keeps it safe even if the program stops working. If there...

6 minutes read.

Loop Statement in C

Loop statement is used to execute one or more statement repeatedly multiple times. There are three types of loops in C language. Why use loop? We can use loop because it executes a...

2 minutes read.

Segmentation Fault in C

Segmentation Fault in C In the C programming language, segmentation fault or segmentation violation or core dump is a condition that the hardware component raises to protect the memory by indicating...

4 minutes read.

Ftell() Function in C

Ftell(): In File Handling we have some special functions like Ftell(), Fseek(), rewind() etc.. while you are randomly accessing the file these functions play very important role and these functions...

3 minutes read.

Local Labels in C

Anyone who has written programs in the C programming language is required to be familiar with the "go to" and "labels" used in C to navigate between functions. "Local labels"...

4 minutes read.

Heap Sort in C

In this tutorial, we will learn about heap sorting in C language, but before going to Heap sort, we have to know the concept of Complete Binary Tree. What is Complete...

8 minutes read.

#include in C

In the C programming language, #include is another way of inferring a standard, or a user defines file into the application or program. The preprocessor of the programming standard generally...

4 minutes read.

Const vs Volatile in C

Introduction Qualifiers are nothing but keywords which are used to modify the properties of a variable. Const and Volatile keywords are qualifiers in C. The Const qualifier is applied to the...

4 minutes read.