cwtests/cw02/factors.while
author Christian Urban <christian.urban@kcl.ac.uk>
Fri, 03 Dec 2021 18:00:30 +0000
changeset 854 ce4a7bab6bd8
parent 853 568671822d52
child 856 23273e3a120f
permissions -rw-r--r--
testcases
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
aad5957eb7e4 cwupdates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     4
write "Input n please";
aad5957eb7e4 cwupdates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     5
read n;
853
568671822d52 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 833
diff changeset
     6
write "\nThe factors of n are";
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 {
aad5957eb7e4 cwupdates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     9
  if ((n / f) * f == n) then  { write(f) } else { skip };
aad5957eb7e4 cwupdates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    10
  f := f + 1
aad5957eb7e4 cwupdates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    11
}