Need some help in Pascal

toph99

Diamond Member
Aug 25, 2000
5,505
0
0
i need to find a way to take an integer input by the user and find all of it's factors. i'm stumped, does anyone have any idea how to do it?
 

royaldank

Diamond Member
Apr 19, 2001
5,440
0
0
It's been 10 years since I did Pascal, but I can at least point you in the right direction.
This will be psuedo code...

Get the input and store as an integer (say, Input)

factorsArray(0) = 1 /* 1 works for everything */
fArrayCount = 1 /* keep track of array indexing */

Range = Input / 2

for i = 2 to Range do /* check all numbers less than half input */
if ((Input % i) = 0 then /* if remainder is 0, it's a factor */
factorsArray(fArrayCount) = i /* store in results array */
end if
next i

print factorsArray /* print the results array, might need to loop through */


------------------------------------------------

Forgive me writing in several different languages...I get them all confused until I actually start writing specific code.

The (Input % i) is the mod function...it should be in Pascal, although I'm not 100% certain. It basically divides the number and return the remainder.

Hope this helps.


 

toph99

Diamond Member
Aug 25, 2000
5,505
0
0
thanks for the reply but it's unfortunatly over my head ;) we just started doing for commands, though maybe my teacher will be able to give me a hand next class(she's away right now)
thanks
 

royaldank

Diamond Member
Apr 19, 2001
5,440
0
0
If that is the case, you can probably get away with doing it like this.

Get input (Input)

print 1 ( Print 1 automatically, because that always works)

Range = Input / 2 (Anything over half wont be a factor)

for i=1 to Range (Test each number in range)
if ((Input%i) = 0) then
print i
end if
next i

print Input

----------------------------------
Basically, I nixed the arrays. This just prints out the factors as it finds them. However, don't forget to print the actual input since any number is a factor of itself.
 

royaldank

Diamond Member
Apr 19, 2001
5,440
0
0
More specifically -

Range := Input

WriteLn("1");

while i<=Range do
begin
if (Input Mod i) then
WriteLn (", ",i);
i := i+1;
end;

WriteLn(Input);

--------------------------------------------

So, all that leaves for you is to declare your varaibles and get the input.
 

toph99

Diamond Member
Aug 25, 2000
5,505
0
0
great, thanks :) i'll try it out tomorrow at school and see how the compiler likes it ;) much appreciated