C Program to Find GCD of Two Numbers using While loop
ADVERTISEMENTS
C program to find gcd of two numbers using while loop. In this article, you will learn how to find gcd of two numbers in c language using while loop.
What is GCD Number?
The GCD is the largest integer number of two positive integer numbers that can exactly divide both numbers without remaining a remainder.
C Program to Find GCD of Two Numbers using While loop
// C Program to Find GCD of Two Numbers using While loop
#include <stdio.h>
int main() {
int p, q;
// p & q - To store the two positive numbers
printf("-----Enter the two positive integer numbers-----\n");
scanf("%d %d",&p, &q);
while (p != q) {
if (p > q) {
p -= q;
} else {
q -= p;
}
}
printf("\nThe GCD number is: %d\n", p);
return 0;
}
Output
-----Enter the two positive integer numbers-----
172
90
The GCD number is: 2
C Program to Find GCD of Two Numbers using For loop
// C Program to Find GCD of Two Numbers using For loop
#include <stdio.h>
int main() {
int p, q, i, g;
// p & q - To store the two positive numbers
// g - To store the GCD number
printf("----Enter the two positive integer numbers-----\n");
scanf("%d %d", &p, &q);
for (i = 1; i <= p && i <= q; ++i) {
if (p % i == 0 && q % i == 0) {
g = i;
}
}
printf("\nThe GCD number of %d & %d is: %d\n", p, q, g);
return 0;
}
Output
----Enter the two positive integer numbers-----
172
90
The GCD number of 172 & 90 is: 2