Ad Code

Finding GCD Using C Programming Language with Examples and Methods.

 GCD in C Language 

Welcome to this tutorial where you will learn how to find the greatest common divisor (GCD) using the C programming language.

The GCD of two numbers is the largest positive integer that divides both numbers without leaving any remainder. It is an important concept in mathematics and has numerous applications in programming, such as in simplifying fractions and finding the LCM of multiple numbers.


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:

Find the greatest common divisor of 30,36, and 24.

            30: 1,2,3,5,6,10,15,30
            36: 1,2,3,4,6,9,12,18,36
            24: 1,2,4,6,12,24

The largest number that appears on every list is 6.
                
            gcd(30,36,24)=6.


How to find GCD using C language:


There are many ways to find the gcd of any two or more numbers using C programming language.

In this program, we will use for loop and you can also find gcd using while and do-while loops.

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;  
 }  

Output:

Enter 1st number: 30
Enter 2nd number: 36

GCD of two numbers 30 and 36 is 6.

Post a Comment

0 Comments

Ad Code