Return to Snippet

Revision: 34581
at October 25, 2010 23:22 by skiabox


Initial Code
//calculate Greatest Common Divisor(GCD)

#include <stdio.h>
#include <stdbool.h>

int main (int argc, const char * argv[]) {
    // insert code here...
	//declarations
	int n , m;
	int gcd;
	int remainder;
    
	//get user input
	printf("Enter two integers: ");
	scanf("%d%d", &n, &m);
	
	//calculations
	while (n != 0)
	{
		remainder = m % n;
		m = n;
		n = remainder;
	}
	
	gcd = m;
	
	//show results
	printf("Greatest common divisor: %d", gcd);

}

Initial URL

                                

Initial Description
Let m and n be variables containing the two numbers.
If n is 0, then stop : m contains the GCD
Otherwise, compute the remainder when m is divided by n.
Copy n into m and copy the remainder into n.
Then repeat the process, starting with testing whether n is 0.

Initial Title
Find greatest common divisor (GCD) between two  numbers

Initial Tags

                                

Initial Language
C