/*
gcf_lcm.c

A couple of useful routines:  gcf (greatest common factor)
and lcm (least common multiple).

Ray Ontko 1999/10/12
*/

#include <stdio.h>

int gcf( int n , int m ) ;
int lcm( int n , int m ) ;

int main()
{
  int a , b ;

  printf( "Enter two integers\n" ) ;
  scanf( "%d" , &a ) ;
  scanf( "%d" , &b ) ;

  printf( "gcf( %d , %d ) = %d\n" , a , b , gcf(a,b) ) ;
  printf( "lcm( %d , %d ) = %d\n" , a , b , lcm(a,b) ) ;

  return 0 ;
}

/*
gcf

Calculates the gcf (greatest common factor) for a pair
of integers.  If either number is zero, zero is returned.
Negative inputs are treated as if they were positive.
*/
int gcf( int n , int m )
{
  if( n == 0 || m == 0 )
    return 0 ;

  if( n < 0 ) 
    n = - n ;
  if( m < 0 ) 
    m = - m ;

  /* subtract the larger from the smaller until they are equal */
  while( 1 ) 
    if( n > m )
      n -= m ;
    else if ( n < m )
      m -= n ;
    else
      break ;

  return n ;
}

/*
lcm 

Calculates the lcm (least common multiple) for a pair
of integers.  If either number is zero, zero is returned.
Negative inputs are treated as if they were positive.
*/
int lcm( int n , int m )
{
  if( n == 0 || m == 0 )
    return 0 ;

  if( n < 0 ) 
    n = - n ;
  if( m < 0 ) 
    m = - m ;

  return n * m / gcf( n , m ) ;
}
