0% found this document useful (0 votes)
18 views3 pages

Extended Euclids Algorithm

Uploaded by

rehna haseena
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views3 pages

Extended Euclids Algorithm

Uploaded by

rehna haseena
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

EXP NO:4

EXTENDED EUCLID’S ALGORITHM

AIM:
To write a C++ program to find the greatest common divisor using euclid’s
theorem.

ALGORITHM:

Step 1: Start

Step 2: Declare the necessary array declarations and header files

Step 3: The input of two varaibles is obtained from the user

Step 4: According to the input given, the gcd of the two numbers is
calculated using the euclid’s theorem.

Step 5: The output is calculated using the Euclid’s formula and is


displayed on the screen

Step 6: Stop

SOURCE CODE:
#include <bits/stdc++.h>

using namespace std;

// Function for extended euclid algorithm

int gcdExtended(int a, int b, int *x, int *y)


{

// Base Case

if (a == 0)

*x = 0;

*y = 1;

return b;

int x1, y1; // To store results of recursive call

int gcd = gcdExtended(b%a, a, &x1, &y1);

// recursive call to calculate the gcd

*x = y1 - (b/a) * x1;

*y = x1;

return gcd;

int main()

// to display the gcd

int x, y, a, b;

cout<<"Enter two numbers:";


cin>>a>>b;

int g = gcdExtended(a, b, &x, &y);

cout << "GCD(" << a << ", " << b

<< ") = " << g << endl;

return 0;

OUTPUT:

RESULT:
The gcd was found and the output was successfully displayed on the screen.

You might also like