×

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

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.

C Math Library

C Math Library The <math.h> header defines various mathematical functions and one macro. Many functions are available in this library to take double as an argument and then return double as...

4 minutes read.

Results of Comparison Operations in C and C++

In this tutorial, we will explore comparison operators and how the system should compare an incoming value supplied as a context parameter to a given value or range of values. A...

2 minutes read.

What is algorithm in C?

What is an algorithm? In computer programming languages, an algorithm is set of statement that solves a particular problem. In C, first step of every algorithm is Start and last step of...

3 minutes read.

Difference between Scope and Lifetime in C

In this article, we will discuss about the key differences between scope and lifetime in C, including their definitions, applications, and how they relate to one another. By the end...

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

How to initialize array to zero in C

In this article, you will learn how to initialise an array to 0 in C In C, an array is declared as: char ZEROARRAY[2022]; The global scope changes at runtime to all zeros....

3 minutes read.

Dynamic memory allocation

Dynamic memory allocation is used to allocate the memory at runtime. It has following function within <stdlib.h> header file. Function Syntax malloc() malloc (number *sizeof(int)); calloc() calloc (number, sizeof(int)); realloc() realloc (pointer_name, number * sizeof(int)); free() free (pointer_name); malloc() function The process...

2 minutes read.

SJF Scheduling Program in C

The SJF (shortest job first) or the shortest job next is the programming scheduling in the C. It is one of the CPU scheduling programming. The SJF is defined as...

4 minutes read.

function pointer as argument in C

Pointers are considered difficult to understand for beginners but pointers can be made to work if you fiddle with them long enough. So, let’s understand this step by step. What are...

3 minutes read.

Bank Account System in C using File Handling

This tells about the creation of bank account system using the C language and handling of files in C. Approach Let us see the approaches and the functions how they are covering...

5 minutes read.

Find a subarray with a given sum.

Find a subarray with a given sum. The simple solution is to recognize all subarrays one by one and to check each subarray's sum. The quick solution follows the following program. Algorithm: From...

4 minutes read.

Booleans in C

Introduction to Boolean With all the complexities of programming, it can take time to understand the basics. One concept, in particular, that confuses many beginners is the use of Booleans in...

6 minutes read.

Why does sizeof(x++) not Increment x in C

Sizeof() is a much-involved operator in C or C++. It is an incorporated time unary administrator which can be utilized to figure the size of its operand. The consequence of...

5 minutes read.

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.

Factorial Program in C using For Loop

Before move on the factorial program. We have to know about the for loop in C programming language. The syntax of the For Loop is: for (initialization statement; test expression; an increment...

3 minutes read.

Command line arguments in C

Command line arguments in C The arguments that are generally passed from the line of command are referred to as command line arguments. These command line arguments are always handled by...

3 minutes read.

Write() function in c

As the name suggests the write () function is used to write the file descriptor. In other words it is used to write any file name without specifying file name,...

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

Stdio.h in C

Header files are used to make the programmer’s efforts a lot easier. In order to make the programming simple, there are a number of libraries which are included as predefined...

4 minutes read.