Posted By

skiabox on 10/25/10


Tagged

common Greatest divisor


Versions (?)

Find greatest common divisor (GCD) between two numbers


 / Published in: C
 

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.

  1. //calculate Greatest Common Divisor(GCD)
  2.  
  3. #include <stdio.h>
  4. #include <stdbool.h>
  5.  
  6. int main (int argc, const char * argv[]) {
  7. // insert code here...
  8. //declarations
  9. int n , m;
  10. int gcd;
  11. int remainder;
  12.  
  13. //get user input
  14. printf("Enter two integers: ");
  15. scanf("%d%d", &n, &m);
  16.  
  17. //calculations
  18. while (n != 0)
  19. {
  20. remainder = m % n;
  21. m = n;
  22. n = remainder;
  23. }
  24.  
  25. gcd = m;
  26.  
  27. //show results
  28. printf("Greatest common divisor: %d", gcd);
  29.  
  30. }

Report this snippet  

You need to login to post a comment.