i'm a novice with swift and wanted to create a method that will take an integer as its parameter and use a fencepost loop to print the factors of that number. This should be separated by the word "and".
For example, the call printFactors(24) should print the following output: 1 and 2 and 3 and 4 and 6 and 8 and 12 and 24
After thinking; i understand how to do it outside of the swift language; but need help with it in swift.
Here is what i had concluded before considering the swift language.
public void printFactors(int n) {
for (int i=1; i <=n; i++) {
if (n % i == 0) {
if (i == 1) {
System.out.print(i);
}
else {
System.out.print(" and " + i);
}
}
}
}
Help is greatly appreciated. Also how would i take the "solution" and output it as on a label? would i set the solution to a var?