×

GCD program of two numbers in C

GCD stands for Greatest Common Divisor, which is also known as Highest Common Factor, which can be abbreviated as HCF. Mathematically it can be defined as any two positive integers having the greatest positive number, which is a common factor of two given positive integers and is known GCD of two numbers. The GCD of two numbers can never be negative or 0, and 1 is the least common factor of two positive integers.

The mathematical process to find GCD:

Using the LCM method, we can obtain the GCD of two numbers by finding the product of two numbers and dividing with the LCM of that two numbers.

Using the Prime factorization method, each integer is written as the product of prime numbers, then find the product of the smallest power of each common factor.

Using Long Division Method, the biggest number in a given set of numbers needs to be divided by the second largest number, and then the second largest number needs to be divided by the remainder of the previous operation. This process continues until the remainder becomes zero. The divisor, which is obtained while doing remainder zero is known as the greatest common divisor of the given positive integers.

This mathematical process of finding GCD can be performed by a system or computer by writing a program.

Program for GCD of Two numbers

#include<stdio.h>
int main()
{
	int a, b;
	printf(“Enter only two positive integers: “);
	scanf(“%d%d”, &a, &b);
	while(a!=b)
	{
		if(a > b)
			a -= b;
		else
			b -= a;
	}
	printf(“GCD of given two numbers is: %d”, a);
	return 0;
} 

Output

GCD program of two numbers in C

The above code can be used as the calculator for finding the common factor of two positive integers, which simplifies the work.

Explanation of the above program:

We use #include<stdio.h> to import standard input and output functions like scanf, printf, puts, gets, etc. The role of it is to declare the integer type variables and used to initialize the variable (the location where we store data), and we can also initialize different datatypes like float (for decimals), char (for letters and symbols), etc.

Here we declared two variables, a and b that are initialized. We used the printf() function to display what to enter, and we used the scanf() function to take two integer values. We used a looping statement called the while() function in which we keep a condition a not equal to b, and we use conditional statements if() and else() functions and specify the condition a greater than b in the if condition. We use some assignment operators who will store the value in a and b.

Note: The while loop is going to be executed until a is equal to b and the print a value.

The different approaches of programming GCD of two numbers:

#include<stdio.h>
int main()
{
   	int a, b, remainder;
   	printf("enter any two numbers:");
   	scanf("%d%d", &a, &b);
   	while(b!=0)
{
      		remainder=a % b;
     		a=b;
      		b=remainder;
   	}
   	printf("GCD of two numbers is given by:%d", a);
   	return 0;
}

Output

GCD program of two numbers in C

Explanation for the above Program:

The above code is another approach to solving the GCD (Greatest Common Divisor) of two numbers by means of remainders. The logic we use is GCD of two numbers is the largest number which exactly divides both the integer with no remainder left.

Initializing three integer values, namely a, b, and remainder. Giving input for two variables, a and b, we are going to iterate the while loop until we get b=0, which means the remainder should become zero. Then we can print that b value which gives the GCD of two numbers.

Program using Functions along with while loop:

#include <stdio.h>  
#include <conio.h>  
INTGCD (int x, int y);   
int main()  
{  
    int x, y, GCD = 0;  
    printf("Enter two positive numbers:");
    scanf ("%d%d", &x, &y);   
    GCD = INTGCD( x, y);  
    printf ( " GCD of the two numbers %d and %d is %d", x, y, GCD);   
    getch();  
}  
  INTGCD ( int x, int y)  
{  
    while (y != 0)  
    {
	  	if ( x > y) 
			x = x - y;  
       	else  
          	y = y - x;  
    }  
    return x;  
}

Output

GCD program of two numbers in C

Related Topics

If Statement in C

Decision Control Statements We frequently desire one set of instructions to be carried out in one circumstance while another set of instructions to be carried out in a completely different circumstance....

3 minutes read.

Advantages of Dynamic Memory Allocation in C

Dynamic Memory Allocation Dynamic memory allocation is a process of assigning or allocating memory to a variable during the execution of a program. Dynamic memory allocation provides storage according to the...

3 minutes read.

Compilation Process in C language

What is the Compilation Process?  The compilation is a method whereby the source code is converted into object code. It is achieved with compiler assistance. The compiler tests the source code for syntactic...

3 minutes read.

Explain the two-way selection in C

In C programming, a two-way selection statement is used to choose between two different options based on a Boolean expression. The two-way selection statement in C is the if statement. The...

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

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.

Exponential() in C

Introduction: In C language, there are available many types of functions. The exponential() function is one of them. It is a mathematical function. This article describes using the pow() property to...

3 minutes read.

Matrix Multiplication in C

Matrix Multiplication in C Matrix multiplication in C: Two matrices can be added, subtracted, multiplied, and divided. To do so, we take input from the consumer for row number, column number, first element matrix,...

3 minutes read.

Explain the Increment and Decrement Operators in C

In the C programming language, the increment operator (++) and the decrement operator (--) are used to increase or decreasing a variable’s value by 1, respectively. The increment operator is written...

10 minutes read.

LCM of two numbers in C

LCM is a mathematical term which stands for Least Common Multiple. LCM of any two numbers is the smallest positive value(number) which is evenly divisible by the two given numbers. Consider...

4 minutes read.

How to delete a file in C

A file is a group of data kept on a secondary device, such as a hard disc. It is typically utilized as a real-world application with a lot of data. These...

3 minutes read.

tolower() Function in C

The tolower() capability is characterized in the ctype.h header document. In the event that the person passed is a capitalized letter set, the tolower() capability switches capitalized letters in order...

3 minutes read.

What is String Comparison in C

String comparison is the process of comparing two strings (sequences of characters) to determine if they are equal, or if one is greater or less than the other. The comparison...

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.

Bank management system in C

This is a mini project that is constructed completely using the C language. The bank management system is a beginner friendly project. To construct this user only needs to know...

12 minutes read.

strcat() Function in C

Strings in c: A string is defined as the set of characters that are enclosed within the double quotations (" "). The String always ends with the null character ("\0"). The...

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

How to measure time taken by a function in C?

Measuring the time taken by a function is quite a complex task, because numerous methods are frequently not transferable to other platforms, measuring the execution time of a C programs...

5 minutes read.

Snake Game in C

Snake game in C is basic desktop console program which was created in 1970’s. It is an old classic game which was played by every child across the world. This...

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.