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.

C Programming Language Function in C Language (Article) Function in C Language (Program)

905

Program:

#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

This Particular section is dedicated to Programs only. If you want learn more about C Programming Language. Then you can visit below links to get more depth on this subject.