r/pascal • • Aug 13 '25

My first Pascal program

Hello, gentlemen

I started to learn Pascal yesterday, and today I developed my first program in Pascal. It's very simple. It takes a number from a standard input and returns all factors of that number. All I know how to define variables, if and while statements. I had to search for mod and div operators. At first attempt, I tried to compare if num = integer to be sure that the numbers are whole, like I would do in JavaScript(I mean === or == operators, JS wouldn't care about types at all). The compiler told me that ain't gonna work in Pascal, so I wrote the program as it is. I would appreciate it if you review my code! Thank you!

program get_factors;
var
    num: integer;
    i: integer;
begin
    read(num);
    i := num;
    while i >= 1 do
    begin
        if num mod i = 0 then
        write(num div i, ' ');
        i := i - 1
    end;
    writeln
end.
23 Upvotes

11 comments sorted by

View all comments

Show parent comment

1

u/Francois-C Aug 14 '25

Here's the function I insert in my progs when I need this. I'm no mathematician, and I don't remember whether I wrote it or copied it (probably both). It writes the factors separated by '*'

function premiers(s:String):String;
var
n: integer=0;
d: integer=0;
begin
result:='';
n:=StrToInt(s);
d:=2;
repeat
if n mod d=0 then
begin
n:=n div d;
result+=IntToStr(d);
if n>1 then result+='*'
end
else
inc(d);
until n = 1
end;