Home / Programs / Write a function that uses a function to find the greatest common divisor (GCD) of two integers. Write C a program to test the function.
Programming Example

Write a function that uses a function to find the greatest common divisor (GCD) of two integers. Write C a program to test the function.

👁 1,010 Views
💻 Practical Program
📘 Step by Step Learning
Write a function that uses a function to find the greatest common divisor (GCD) of two integers. Write C a program to test the function.

Program Code

#include <stdio.h>

int GCD(int,int);

int main(void)
{
	int nOne, nTwo, n;
	printf("\n Enter two numbers: ");
	scanf("%d %d", &nOne, &nTwo);
	n=GCD(nOne,nTwo);
	printf("\n GCD of %d and %d is %d \n",
							nOne,nTwo,n);
	return 0;
}
int GCD(int x,int y)
{
	int result=1, k=2;
	while(k<=x && k<=y)
	{
		if(x%k==0 && y%k == 0)
				result=k;
		k++;
	}
	return result;
}
 

Output


 Enter two numbers: 12
6

 GCD of 12 and 6 is 6
 

Explanation

None

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.