title: GCD function for C
body: <p>Q 1. Problem 5 (evenly divisible) I tried the brute force method but it took time, so I referred few sites and found this code:</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  int gcd(int a, int b)
  {
      while (b != 0)
      {
          a %= b;
          a ^= b;
          b ^= a;
          a ^= b;
      }
  
      return a;
  }
  
  int lcm(int a, int b)
  {
      return a / gcd(a, b) * b;
  }
  
  int main()
  {
      int res = 1;
      int i;
      for (i = 2; i &lt;= 20; i++)
      {
          res = lcm(res, i);
      }
  
      printf("%d\n", res);
      return 0;
  }
  </code></pre>
  
  <p>This is very simple but I don't understand how function "gcd" works; can somebody please help me understand the logic. (I know it returns the GCD of 2 numbers but why so many operations?)</p>