% fraction_format.m
%
%  Given integers P and Q, print the fraction P/Q in lowest terms
%  and standard form.
%
  p = input ( '  Enter denominator P: ' );
  q = input ( '  Enter numerator   Q: ' );
%
%  We can't accept a numerator of 0.
%
  if ( q == 0 )
    error ( '  Q = 0 is an illegal numerator.' )
  end
%
%  Divide out the GCD.
%
  r = gcd ( p, q );

  p = p / r;
  q = q / r;
%
%  Q should not be negative.
%
  if ( q < 0 )
    p = - p;
    q = - q;
  end
%
%  P/Q is in standard form.
%
  if ( q == 1 )
    fprintf ( '  Standard form for P/Q is %d\n', p );
  else
    fprintf ( '  Standard form for P/Q is %d / %d\n', p, q );
  end
