Problem Statement:
Write a function to compute the greatest common divisor
given by Euclid’s algorithm, exemplified for J = 1980, K =
1617 as follows:
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.
given by Euclid’s algorithm, exemplified for J = 1980, K =
1617 as follows:
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.
Solution:
//Ahmad Furqan
//P5.k
#include <iostream>
#include <conio.h>
using namespace std;
int gcd_euclid(int j, int k)
{
int a, b;
while (k > 0)
{
a = j / k;
b = k;
k = j - a*k;
j = b;
}
return j;
}
void main(void)
{
int j, k;
cout << "Enter a number: ";
cin >> j;
cout << "Enter second number: ";
cin >> k;
cout << "Greatest common divisor of " << j << " and " << k << " is: " << gcd_euclid(j, k);
_getch();
}
I need chapter 7 problem 8 solution
ReplyDelete