The software you can download is much more powerful than the following code, but the program is based on a simple idea: recursion, which is shown below.

 
/************************************************
 *  A basic GCD Finder                          *
 *  programming by Allen Lam                    *
 ************************************************/

#include <stdio.h>
#include <stdlib.h>

int getgcd(int m, int n){
  int result;
  if (m%n==0) result = n; else
    result = getgcd(n, m%n);
  return result;
}

int main(){
  int a, b;
  printf("GCD Finder\n\n");
  printf("Enter two numbers (seperated by space): ");
  scanf("%d %d", &a, &b);
  printf("GCD is %d\n", getgcd(a, b));
  return 0;
}

/************************************************
How to find LCM?
You can write it yourself by using a formular:

LCM * GCD = m * n

(GCD is the same thing as HCF)
 ************************************************/

back