×

GCD program in C

C language :

Dennis Ritchie developed the general-purpose computer language C at Bell Laboratories in 1972. Despite being an ancient language, it is extremely popular.

It is among the most widely used programming languages worldwide. Knowing C will make it easy for you to learn other popular programming languages like Java, Python, C++, C#, etc. because of how similar their syntax is.

Compared to other programming languages like Java and Python, C is very quick. It is possible to use C in both applications and technologies, making it incredibly adaptable.

GCD  ( Greatest Common Divisor ) :

The greatest common factor (GCF) that divides two or more numbers is known as the greatest common divisor (GCD). The highest common factor is another name for it (HCF). For instance, since both 15 and 10 can be divided by 5, 5 is the biggest common factor between both.

Mathematical Approach:

To find greatest common divisor using the mathematical approach we can find the greatest common divisor ( gcd ) using three different ways as shown below :

  • Prime factorization method
  • Euclid ‘ s division algorithm
  • Long division method

The greatest common divisor of two numbers, a and b, is shown by the symbol gcd (a, b). We must list all the factors of the numbers and identify the biggest common factor in order to determine the gcd of the numbers.

Let's say there are three numbers: 4, 8, and 16. The factors 4, 8, and 16 are then:

                                  4 => 1,2,4

                                  8 ⇒ 1,2,4,8

                                 16 ⇒ 1,2,4,8,16

As a result, we can say that among the three numbers, 4 is the greatest common factor.

Real life applications of greatest common divisor (GCD ) :

In numerous instances in real life, as seen below, the idea of the greatest common factor or divisor is applied.

A store owner must pack 420 balls and 130 bats in a single day. She wants to pack them such that each set fits the same number of times in a box and takes up the smallest amount of space. What is the maximum number that can be included in each set for the purpose of packaging?

The needed integer in the above mentioned equation will be the greatest common divisor of 420 and 130.

Other applications include grouping people into smaller parts, placing students in rows and columns of an equal number, etc.

GCD (Greatest Common Divisor) program in C programming language :

We can find The GCD of the two given numbers in C language in three different ways as shown below:

  • For loop and if else statement
  • While loop and if else statement
  • Using recursion

Now let us see how to find the greatest common divisor using for loop and if else condition using following code as shown below:

Example 1:

# include < stdio . h >
# include < conio . h >
void main ( void )
{
int a , b , i , ans;
printf ( “ Enter the value of integers “ );
scanf ( “ % d  % d “ , &a , &b );
for ( i = 1 , i <= a && i <= b , i++ )
{
if ( ( a % i == 0 ) && ( b % i == 0 ) ) 
{
ans = i;
} // if
} // for loop
printf ( “ GCD of % d and % d is : % d “ , a , b , ans );
} // main function

Input 1 and Output 1:

Enter the value of integers 
10
45
GCD of 10 and 45 is : 5

Now let us see how to find the greatest common divisor using while loop and if else condition using following code as shown below :

Example 2:

# include < stdio . h >
# include < conio . h >
void main ( void )
{
int a , b ;
printf ( “ Enter the value of integers “ );
scanf ( “ % d  % d “ , &a , &b );
while ( a != b )
{
if ( a> b)
{
a -= b ;
} // if block 
else
{
b -=a ;
} // else block
} // while loop
printf ( “ GCD of % d and % d is : % d “ , a , b , a);
} // main function

Output 2:

Enter the value of integers 
9
81
GCD of 10 and 45 is : 9

Now let us see how to find the greatest common divisor using the concept of recursion using following code as shown below :

Example 3:

# include < stdio . h >
# include < conio . h >
int gcd ( int a , int b );
void main ( void )
{
int a , b ;
printf ( “ Enter the value of integers “ );
scanf ( “ % d  % d “ , &a , &b );
printf ( “ GCD of % d and % d is : % d “ , a , b , gcd ( a , b ) );
} // main method
int gcd ( int a , int b )
{
if ( b != 0)
{
return gcd ( b , a % b );
} // if block
else
{
return a ;
} // else block
} // gcd method

Output 3 :

Enter the value of integers 
16
38
GCD of 10 and 45 is : 2

Related Topics

Escape Sequence in C

An escape sequence is a sequence of character that used inside the string literal or character.  All the escape sequence is used with the backslash (\) symbol. The list of an...

1 minute read.

Math functions in C

Math functions in C: In the C programming language, the compiler allows the developer to perform mathematical operations through the header’s functions, represented by <math.h>. The <math.h> header defines various mathematical...

3 minutes read.

C Pointers

Pointers in C A pointer is a variable, which contains the address of another variable, i.e., it’s a variable which has the address of another variable as its value. The utilization...

4 minutes read.

Bigint (BIG INTEGERS) in C with Example

The maximum number of digits that a long, long int can have in C/C++ is 20. The issue is how to store the 22-digit number, which is difficult to do...

9 minutes read.

Nested if-else statement in C

If we use an if-else statement within another if statement in a C program, it is called a nested if-else statement in C. It helps to check the condition inside...

3 minutes read.

What is Linked List in C

Linked list Similar to an array, a linked list is a linear data structure that stores a chain of nodes with two fields of memory in each node. Where first memory...

7 minutes read.

C Program of Fencing the Ground

The college's ground is rectangular. Fencing the ground the management makes the decision to construct a fence around the ground. They planned to wrap a thick rope around the ground...

1 minute read.

How to Merge Array in C?

Introduction Merging arrays is a common task for many developers but might be difficult for beginners of C programming. Fortunately, our guide will show you how to quickly merge arrays in...

6 minutes read.

C/C++ Program to Find the Size of int, float, double and char

In this tutorial, we will learn how to use the sizeof operator to determine the size of each variable. Program to Determine Variable Size Write a C or C++ program to determine the...

2 minutes read.

Find reverse of an array in C using functions

Introduction: Here, we describe how to find the reverse array in C using functions. Suppose you have an array with n elements. I need to display the elements present in...

3 minutes read.

If else Programs in C Programming

If else Programs in C programming The if-else statement in C is based on some particular conditions to perform the operations. If and only if the given condition is valid, the operations listed in...

4 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.

Float in islower() in C

Introduction: This article briefly discusses the float in islower() in C. The islower() function checks if the input character passed inside the function is lowercase. Lowercase letters include (a-z). The islower()...

4 minutes read.

Limitations of Inline Function in C

The compiler is unable to conduct inlining in two instances. It just accepts the inline definitions and produces space for the function in the same way it does for a...

3 minutes read.

C Data type

Data Types in C with Examples Data types are used to define the type of data that a variable can store to perform a specific operation.ANSI C provides three types of...

2 minutes read.

How to use floor() function in C

Introduction: The floor() used as a function in the C programming language. This function uses to return the largest or most significant integer value. The integer value is smaller than...

3 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.

Use of free() function in C

Introduction The free() function uses in C programming language. The free() function in the C programming language uses to release or deallocate the memory blocks; these blocks are previously allocated by calloc(), malloc() or realloc()...

3 minutes read.

Flow chart of While loop in C

This is a flowchart that represents the process of executing the while loop in the C programming language.Generally, as we know there are three main components of while loop:1. The...

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.