×

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

Java Array vs ArrayList

As we all know, arrays are linear data structures that allow you to add elements to them continuously in memory address space, whereas ArrayList is a Collection framework class. Despite...

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.

DSA Program in C

How is a Data Structure Implemented? The foundational terms of a data structure are the following terms. Data may be arranged using data structures to make it easier to use. Each data...

20 minutes read.

C printf and Scanf

Input-Output functions in C Programming In C Language, the printf() and scanf() are inbuilt library functions that used for input and output. It is defined in the header file“<stdio.h>”. printf() Function: In C...

2 minutes read.

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

4 minutes read.

What is 2s Complement in C?

The complement of 2s in C is generated from the 1s in C. As we know, the 1s complement of a binary number is generated by converting bit 1 to...

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

Control statement in C

What is a control statement? A control statement helps us to control the flow of the program. The control statement helps us to execute the program's instructions in a user-defined order....

8 minutes read.

Memory leak in C

What is memory leak in C? Memory leak occurs when we keep allocating memory in the heap without freeing it, i.e., the allocated memory in heap is not released back to...

3 minutes read.

Modulus on Negative Numbers in C

In this tutorial, we will explore some examples of modulus on negative number. What is Modulus of Negative number? By omitting the minus sign, one can determine the modulus of a negative...

3 minutes read.

strtok() and strtok_r() functions in C with examples

strtok() and strtok_r() functions in C with examples C offer various strtok() and strtok r() methods for a string to be separated by a certain delimiter. Dividing a string is a...

2 minutes read.

Reverse a Stack using Recursion in C

In this tutorial we will learn how recursion will be used in this case to reverse a stack. For loops, while loops, do-while loops, and similar constructions are not permitted....

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

C and C++ Binary Files

What is a binary file? A file in which the content is written in binary format is called a binary file. A binary file is not a text file. There are...

7 minutes read.

Different ways to Declare the Variable as Constant in C

There are a wide range of ways of making the variable as steady Using const keyword: The const catchphrase permits a software engineer to inform the compiler that a specific variable should...

5 minutes read.

Pure Virtual Function in C

Before knowing about the pure virtual function some of the important points about the virtual function in C++ are given below. In c++ the member function that is defined in the...

4 minutes read.

Storage class in C

Storage class in C defines the scope, the visibility, and the lifetime of variables and functions. In other words, storage classes are used to describe the features of variables and...

3 minutes read.

C Decision control statement

Decision Making C conditional statements are used to specify one or more conditions. The statements will be executed if the condition becomes true otherwise alternative statement will be executed if the...

3 minutes read.

Tower of Hanoi in C

What is Tower of Hanoi? The Tower of Hanoi is a gaming problem that was created in 1883 by a French mathematician named Édouard Lucas. The Tower of Hanoi temple in...

4 minutes read.

Conditional Operator in C

In C programming language, the conditional operator (also known as the ternary operator) is a shorthand way of writing an if-else statement. The syntax of conditional operator in C is...

3 minutes read.