×

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

Isalnum() function in C

Introduction: The isalnum () is a function used in C programming language. This function checks the passing number or argument is an alphanumeric number or not. The alphanumeric number consists of the...

3 minutes read.

What is a buffer in C?

A buffer is an area of memory set aside for the temporary storage of data. A data buffer (or just buffer) is a region of a physical memory storage used to...

5 minutes read.

Merge Sort in C

The merge sort follows the principle of the divide and conquers algorithm. Firstly it divides the input of an array into two halves and then calls itself for the two...

4 minutes read.

Union Program in C

 How should a union be defined?  In C programming, a Union is a method used to organize different data types into groups in the same way that Structures are used. We...

4 minutes read.

Library Functions in C

What are library functions? Library functions are the built-in functions (functions provided by the system) which are stored in the library. The library is the common location where these functions are...

3 minutes read.

Simple Programs in C Language

In this article, we will discuss the basic programs in C language. Addition of two values in C. Swapping two values. Finding the odd or even values. Finding vowels and consonants in C. Finding the...

11 minutes read.

How to Calculate Time Complexity in C?

What is time complexity? An algorithm's time complexity measures how long it takes to complete a task in relation to the size of the input. It should be noted that the...

5 minutes read.

FIFO Example in the C Language

FIFO is an acronym that means "First In, First Out." It is a data structure handling method in which the first element is handled first, and the last element is...

3 minutes read.

Decimal to Hexadecimal in C

Decimal to Hexadecimal in C Let us first understand the definitions of decimal and hexadecimal. In algebra, a decimal number is defined as a number whose whole number part and a...

3 minutes read.

Queue implementation in C

In the C programming language, the queue is the abstract concept that is similar to stacks. However, it is the part of the data structures that work opposite to that...

4 minutes read.

How to create a node in C?

A node is a small concept taken from the graph theory, or a node is a fundamental unit of a data structure, like a tree data structure. Every graph contains...

3 minutes read.

C program to Store Information of Students Using Structure

What is the Structure in C? User-defined data types include structures. Structures aid your ability to combine things of various categories into a single group. Like arrays, it operates similarly. A...

3 minutes read.

Pyramid Pattern in C

Pyramid Pattern in C A pyramid is a polyhedron created by connecting a base that will be a polygon, and all the lateral faces are triangles. All the bases are connected...

5 minutes read.

Bit Stuffing Program in C

What is a Bit Stuffing? The process of inserting one or more extra bits (0) into data is known as bit stuffing. To provide signalling information to a recipient, these are...

3 minutes read.

memcmp() in C

Introduction: In this article we are discuss about memcmp() function in C. This function permits the person to evaluate the bytes of the two characters, as mentioned above. Depending on...

4 minutes read.

Passing Array to Function in C

Need to pass Arrays? The need to pass arrays to a function arises when we need to pass a list of values to a given function. During the course of our programming...

4 minutes read.

Nested Loops in C Programming Examples

A nested loop is generally used when we want to run a loop statement inside another loop statement. This kind of loop is also known as a “loop inside the...

6 minutes read.

Diffie-Hellman Algorithm in C

Background: A method of public-key encryption known as elliptic curve cryptography (ECC) is based on the algebraic structure of elliptic curves over finite fields. To ensure equal security, ECC encryption requires fewer...

4 minutes read.

Top Array Keywords in C

Sorting array in C Types of array in C Array of structure in C How do you initialize an array in C Array declaration in C Array of strings in C Multidimensional array in C Reverse array...

1 minute read.

C Program to find the size of a File

Before knowing about the Program, you need to understand what the size of a file is. After that, you should know how it is going to work with the given...

4 minutes read.