Write a C program for the following requirement thank you Wr
Write a C program for the following requirement, thank you! Write a program that takes three composite (Non-prime) positive integers from the user and computes their greatest common divisor (gcd).
Solution
#include <stdio.h>
int main(void)
{
int n1, n2, n3, i, gcdtwo,gcdthree;
printf(\"Enter three composite numbers: \");
scanf(\"%d %d %d\", &n1, &n2, &n3);
for(i=1; i <= n1 && i <= n2; ++i)
{
// Checks if i is factor of both n1 and n2
if(n1%i==0 && n2%i==0)
gcdtwo = i;
}
for(i=1; i <= gcdtwo && i <= n3; ++i)
{
// Checks if i is factor of both gcdtwo, the gcd of two numbers and n3
if(gcdtwo%i==0 && n3%i==0)
gcdthree = i;
}
printf(\"G.C.D of %d ,%d and %d is %d\", n1,n2,n3, gcdthree);
return 0;
}
output:
