Pascal: Factors

program ex23;

var   num, half_num, i: integer;

begin
  writeln ('Enter a number to find its factors');
  readln (num);
  half_num := num div 2;
  for i:=1 to half_num do
    if (num mod i = 0) then
      writeln (i);
  readln;
end.

To discover if one number is a factor of another we need to know whether the remainder after division is zero. We don't need to go beyond half the number because half the number is the largest possible factor.

Back to questions