function F = fact(n)
F = 1;

if ( n < 0 || n > fix(n) ) 
    fprintf('Error in factorial: bad argument value %g\n', n);
    return;
elseif ( n == 0 || n == 1) % This 2nd check is actually unnecessary  
    return;                % the program would work without it
end;                       

for k = 2:n
    F = F * k;
    if (F == Inf) 
       fprintf('Infinity in factorial: argument %g too big\n', n);
       return;
    end
end
