by CodeChum Admin
The greatest common divisor, also known as GCD, is the greatest number that will, without a remainder, fully divide a pair of integers.
Now, I want you to make a program that will accept two integers and with the use of loops, print out their GCD. Make good use of conditional statements as well.
Off you go!
Input
1. First integer
2. Second integer
Output
The first line will contain a message prompt to input the first integer.
The second line will contain a message prompt to input the second integer.
The last line contains the GCD of the two integers.
Enter·the·first·integer:·6
Enter·the·second·integer:·9
GCD·of·6·and·9·is·3
#include <stdio.h>
int main()
{
int n1, n2, i, gcd = 0;
printf("Enter the first integer: ");
scanf("%d", &n1);
printf("Enter the second integer: ");
scanf("%d", &n2);
for (i = 1; i <= n1; i++)
{
if (n1 % i == 0 && n2 % i == 0)
gcd = i;
}
printf("GCD of %d and %d is %d\n", n1, n2, gcd);
return 0;
}
Comments
Leave a comment