0% found this document useful (0 votes)
17 views4 pages

Extended gcd-067

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

Extended gcd-067

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

Exp.

no: GCD using Extended Euclidean algorithm


Date:

Aim:

To write a c code for execute GCD and Multiplicative inverse using Extended Euclidean algorithm.

Algorithm:
Step 1: Define a function to compute the GCD using the Extended Euclidean algorithm.

Step 2: Prompt the user to enter two integers.

Step 3: Read the integers from the user.

Step 4: Compute the GCD of the integers and print the result.

Step 5:Compute the Multiplicative inverse and print the result.

Program:
#include <stdio.h>

int extendedGCD(int a, int b, int *x, int *y) {

if (b == 0) {

*x = 1;

*y = 0;

return a;

int x1, y1;

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

*x = y1;

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

return gcd;

int modularInverse(int a, int m) {


int x, y;

int gcd = extendedGCD(a, m, &x, &y);

if (gcd != 1) {

return -1;

int inverse = (x % m + m) % m;

return inverse;

int main() {

int a, b;

printf("Enter two integers (a and b): ");

scanf("%d %d", &a, &b);

int x, y;

int gcd = extendedGCD(a, b, &x, &y);

int inverse = modularInverse(a, b);

printf("GCD of %d and %d is: %d\n", a, b, gcd);

if (inverse != -1) {

printf("Multiplicative inverse of %d modulo %d is: %d\n", a, b, inverse);

} else {

printf("Multiplicative inverse does not exist for %d modulo %d\n", a, b);

printf("RRN:220171601067\n");

return 0; }

Output:
Review Questions:

Result:
Thus, the program to execute GCD and Multiplicative inverse using Extended Euclidean algorithm
has been successfully verified and executed.

You might also like