gcd of two numbers in c program

Today, We want to share with you gcd of two numbers in c program.In this post we will show you GCD Using for loop and if Statement, hear for Find GCD of two Numbers we will give you demo and example for implement.In this post, we will learn about GCD Using while loop and if-else Statement with an example.

gcd of two numbers in c program

There are the Following The simple About GCD for both positive and negative numbers Full Information With Example and source code.

As I will cover this Post with live Working example to develop greatest common denominator, so the how to find greatest common denominator is used for this example is following below.

c programming – Find GCD of two Numbers

some Good Examples with easy way to all about of the GCD Tutorials on different ways to calculate GCD of 2 datatype of the nintegers (for two positive as well as negative integers) using loops as well as if else making some statements.

gcd of two numbers in c program
gcd of two numbers in c program

Examples on different ways to calculate greatest common divisor (gcd) of two integers (vlaue of both positive and negative integers ) using loops and decision making statements in C programing.

GCD stands for Greatest Common Divisor & HCF stands for Highest Common Factor.GCD or HCF of two interger numbers is the big or large number that some divides two of them.

hear we will show you 3 different way to find greatest common divisor (gcd).Simple calculate and method of GCD

82 = 21 411 
152 = 23 191 

The answer is: 21  = 2

C Program to Find GCD of two Numbers

Example 1:

GCD Using for loop and if Statement

#include 
int main()
{
    int first, second, i, gcd;

    printf("Enter two integers: ");
    scanf("%d %d", &first, &second);

    for(i=1; i <= first && i <= second; ++i)
    {
        // Checks if i is factor of both integers
        if(first%i==0 && second%i==0)
            gcd = i;
    }

    printf("G.C.D of %d and %d is %d", first, second, gcd);

    return 0;
}

Example 2:

GCD Using while loop and if...else Statement

#include 
int main()
{
    int first, second;
    
    printf("Enter two positive integers: ");
    scanf("%d %d",&first,&second);

    while(first!=second)
    {
        if(first > second)
            first -= second;
        else
            second -= first;
    }
    printf("GCD = %d",first);

    return 0;
}

Example 3:

GCD for both positive and negative numbers

#include 
int main()
{
    int first, second;

    printf("Enter two integers: ");
    scanf("%d %d",&first,&second);

    first = ( first > 0) ? first : -first;
    second = ( second > 0) ? second : -second;

    while(first!=second)
    {
        if(first > second)
            first -= second;
        else
            second -= first;
    }
    printf("GCD = %d",first);

    return 0;
}

gcd of two numbers in c program

C program to find GCD of two numbers, Recursive function to return gcd of first and second, Driver program to test above function

 
#include  

int gcd(int first, int second) 
{ 
	if (first == 0) 
	return second; 
	if (second == 0) 
	return first; 

	if (first == second) 
		return first; 
	if (first > second) 
		return gcd(first-second, second); 
	return gcd(first, second-first); 
} 

int main() 
{ 
	int first = 98, second = 56; 
	printf("GCD of %d and %d is %d ", first, second, gcd(first, second)); 
	return 0; 
} 

gcd of two numbers in C++ program

 
#include  
using namespace std; 
int gcd(int first, int second) 
{ 
	if (first == 0) 
	return second; 
	if (second == 0) 
	return first; 

	if (first == second) 
		return first; 

	if (first > second) 
		return gcd(first-second, second); 
	return gcd(first, second-first); 
} 
 
int main() 
{ 
	int first = 98, second = 56; 
	cout<<"GCD of "<

gcd of two numbers in Java program


class Test 
{ 
	static int gcd(int first, int second) 
	{ 
		if (first == 0) 
		return second; 
		if (second == 0) 
		return first; 
	
		if (first == second) 
			return first; 
		if (first > second) 
			return gcd(first-second, second); 
		return gcd(first, second-first); 
	} 
	
	public static void main(String[] args) 
	{ 
		int first = 98, second = 56; 
		System.out.println("GCD of " + first +" and " + second + " is " + gcd(first, second)); 
	} 
} 

gcd of two numbers in Python 3 program

 
def gcd(first,second): 
	
	if (first == 0): 
		return second 
	if (second == 0): 
		return first 

	if (first == second): 
		return first 
 
	if (first > second): 
		return gcd(first-second, second) 
	return gcd(first, second-first) 

first = 98
second = 56
if(gcd(first, second)): 
	print('GCD of', first, 'and', second, 'is', gcd(first, second)) 
else: 
	print('not found') 

gcd of two numbers in C# program

using System; 

class GFG { 

	static int gcd(int first, int second) 
	{ 
		
		if (first == 0) 
		return second; 
		if (second == 0) 
		return first; 

		if (first == second) 
			return first; 
	
		if (first > second) 
			return gcd(first - second, second); 
			
		return gcd(first, second - first); 
	} 

	public static void Main() 
	{ 
		int first = 98, second = 56; 
		Console.WriteLine("GCD of "
		+ first +" and " + second + " is "
					+ gcd(first, second)); 
	} 
} 

gcd of two numbers in PHP program

 $second) 
		return gcd( $first-$second , $second ) ; 

	return gcd( $first , $second-$first ) ; 
} 

$first = 98 ; 
$second = 56 ; 

echo "GCD of $first and $second is ", gcd($first , $second) ; 

?> 
Web Programming Tutorials Example with Demo

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about algorithm to find gcd of two numbers in c.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment