0% found this document useful (0 votes)
8 views2 pages

Practical 4

The document outlines a practical exercise to implement Euclid's algorithm for finding the GCD of two numbers. It includes a C code example that demonstrates the algorithm and its steps. The program prompts the user to input two numbers and calculates their GCD while displaying the steps involved.

Uploaded by

kriyank
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)
8 views2 pages

Practical 4

The document outlines a practical exercise to implement Euclid's algorithm for finding the GCD of two numbers. It includes a C code example that demonstrates the algorithm and its steps. The program prompts the user to input two numbers and calculates their GCD while displaying the steps involved.

Uploaded by

kriyank
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/ 2

Enrollment No.

:230843116012

PRACTICAL – 4
AIM:- Implement Euclid algorithm to find GCD.
GCD(16,12) = 4
GCD(12,4) = 0
Then 4 is the GCD(16,12)

CODE:-
#include <stdio.h>
#include <stdlib.h>

int gcd(int a, int b) {


if (b == 0) {
return a;
}

return gcd(b, a % b);


}

void printGCDSteps(int a, int b) {


printf("GCD(%d,%d) = ", a, b);

if (b == 0) {
printf("%d\n", a);
return;
}

printf("%d\n", a % b);
printGCDSteps(b, a % b);
}

int main() {
int a, b;

printf("Euclidean Algorithm for GCD\n");


printf("==========================\n\n");

printf("Enter two numbers to find their GCD: ");


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

printf("Calculating GCD(%d,%d):\n", a, b);


printGCDSteps(a, b);
printf("Then %d is the GCD(%d,%d)\n", gcd(a, b), a, b);

return 0;

15
Enrollment No.:230843116012

OUTPUT

16

You might also like