GCD in C Language
In this tutorial, we will provide you with a comprehensive guide to finding the GCD of two numbers using C programming language. We will cover different methods to calculate the GCD, including the Euclidean algorithm, which is the most efficient and widely used method.
You will learn how to write a C program to find the GCD of two numbers using loops and recursion, and we will also provide you with clear explanations and easy-to-understand examples.
By the end of this tutorial, you will have a thorough understanding of how to find the GCD using C programming language and the different methods involved.
So whether you are a beginner or an experienced programmer, this tutorial will equip you with the knowledge and skills needed to perform GCD calculations efficiently in your programming tasks.
What is GCD?
GCD stands for Greatest Common Divisor. It is a mathematics term of two or more numbers. it is also known as the Highest Common Factor (HCF) of two numbers.
Example:
How to find GCD using C language:
Source Code:
#include <stdio.h>
#include <conio.h>
int main()
{
int num1, num2, i, GCD;
printf ( " Enter 1st number: \n ");
scanf ( "%d", &num1);
printf ( " Enter 2nd number: \n ");
scanf ( "%d", &num2);
for( i = 1; i <= num1 && i <= num2; ++i)
{
if (num1 % i ==0 && num2 % i == 0)
GCD = i;
}
// print the GCD of two numbers
printf (" GCD of two numbers %d and %d is %d.", num1, num2, GCD);
return 0;
}
0 Comments