Answer to Question #340188 in C for Tanaka

Question #340188

6. The GCD

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
1
Expert's answer
2022-05-14T16:42:11-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS