Chapter - 6: Functions And Pointers

Write a function to compute the greatest common divisor given by Euclid’s algorithm, exemplified for J = 1980, K = 1617 as follows:


G
Sections
12
Exercises

1980 / 1617 = 1 1980 – 1 * 1617 = 363
1617 / 363 = 4 1617 – 4 * 363 = 165
363 / 165 = 2 363 – 2 * 165 = 33
5 / 33 = 5 165 – 5 * 33 = 0

Thus, the greatest common divisor is 33.


#include<stdio.h>
#include<conio.h>

int gcd(int, int);

int main()
{
	int a, b, cd, max, min;
	printf("Enter two numbers : ");
	scanf("%d%d", &a, &b);
	if (a>b)//for making a greater number
	{
		max = a;
		min = b;
	}
	else
	{
		max = b;
		min = a;
	}
	a = max;
	b = min;
	cd = gcd(a, b);//returning the greatest divisor
	printf("\n\nGreatest common divisor of the givn numbers is %d", cd);
	getch();
	return 0;
}

int gcd(int a, int b)
{
	static int x, temp;
	if (b == 0)
		return (a);
	x = a / b;
	temp = a;
	a = b;
	b = temp - x*b;
	gcd(a, b);
}

© 2021 Garbage Valuegarbage value logo