×

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

Multithreading in C interview questions

Q1. What exactly is a thread? Ans: A thread is a brief sequence of instructions intended to be planned and carried out by the CPU apart from the parent process. Q2. Can...

4 minutes read.

Strcpy() in C

In C language many operations can be performed on a string. Characters in a sequence are known as string. Note:   To use this function we must include the #include<string.h> header file...

4 minutes read.

Array Example in C

An array is a collection of similar types of data elements arranged in such a way that any number of values can be assigned to it. It can store values that...

4 minutes read.

Write a program that produces different results in C and C++

In this tutorial, we'll explore several programs that, depending on whether they are developed using C or C++ compilers, will produce varying outputs. There are numerous similar programs, but we will just...

2 minutes read.

memcpy() in C

Introduction: Here we discuss about the memcpy() function in C. The memcpy() function is also called the Copy Memory Block function. Used to create a copy of a specific drawing...

4 minutes read.

Pointer arithmetic in C

In the C programming language, a pointer is an address which stores a numeric value. Hence, a developer can perform several arithmetic operations on the same just as one does...

4 minutes read.

C Program to Find Largest Number Using Dynamic Memory Allocation

This tutorial will teach us how to locate the highest number a user has ever input into a dynamically allocated memory. C Program: #include <stdio.h> #include <stdlib.h> int main ()  {   int no;   double *data_n;   printf...

1 minute read.

Formatted Input and output function in C

Formatted I/O functions display multiple outputs to the user by taking various inputs. All data types like int, float, char and double are supported by the formatted I/O function. In...

4 minutes read.

Comments in C

Comments are used to comment on the line of code in the program. Comments are a way of inserting remarks and reminders into code without affecting its behavior. The compiler...

1 minute read.

Hook() function in C

Use of hook () function in C Introduction:  The hook () is a function used in the C programming language. The basic concept of the hook() function is that it can remove the function...

3 minutes read.

C program to compare the two strings

C program to compare the two strings Strings can be compared either by using the string function or without using string function. First, we will look at how we can compare the strings with...

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.

C pre-processor

The C processor is a macro processor which is used to compile the source code of the program (step by step) . It is not a part of the compiler....

4 minutes read.

Round Robin Scheduling in C

Round Robin Scheduling in C Round robin is a CPU (Central Processing Unit) scheduling algorithm designed to share the time systems. It is one of the simplest and easiest scheduling algorithms...

4 minutes read.

Caesar Cipher Program in C

What is Caesar Cipher? The Caesar Cipher is a type of substitution cipher that is named after Julius Caesar, who is said to have used it to encrypt messages sent to...

2 minutes read.

C program that does not Suspend when Ctrl+Z is Pressed

In Programming when a program breakdown and runs surprisingly in the terminal compiler the developer has the ability to prevent the program from running unequivocally. For expressly halting the program,...

3 minutes read.

C Language Environment Setup

To compile C program, we must have GCC compiler installed on our machine. In this C tutorial, all the examples are compiled and tested using GCC compiler. Although we can...

3 minutes read.

Attributes in C

Attributes are one of modern C++ key features. It is the process by which initiator can add more information to the language instead of introducing new keywords for every attribute....

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.

Cbrt() function in C

Introduction: The Cbrt is a function used in C programming language. The cbrt() function is a math function. Using the cbrt function, we can do the cube root of a function. This...

4 minutes read.