cwtests/cw02/factors.while
author Christian Urban <christian.urban@kcl.ac.uk>
Mon, 25 Sep 2023 13:14:34 +0100
changeset 928 2f3c077359c4
parent 860 6f80e6df34f7
child 934 ee35eeb5831a
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
833
aad5957eb7e4 cwupdates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     1
// Find all factors of a given input number
aad5957eb7e4 cwupdates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     2
aad5957eb7e4 cwupdates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     3
860
6f80e6df34f7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 856
diff changeset
     4
write "Input n please: ";
833
aad5957eb7e4 cwupdates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     5
read n;
860
6f80e6df34f7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 856
diff changeset
     6
write "The factors of n are: \n";
833
aad5957eb7e4 cwupdates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     7
f := 2;
aad5957eb7e4 cwupdates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     8
while (f < n / 2 + 1) do {
860
6f80e6df34f7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 856
diff changeset
     9
  if ((n / f) * f == n)
6f80e6df34f7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 856
diff changeset
    10
  then { write(f); write("\n") }
6f80e6df34f7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 856
diff changeset
    11
  else { skip };
833
aad5957eb7e4 cwupdates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    12
  f := f + 1
aad5957eb7e4 cwupdates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    13
}