Hi! I was working on a project for my class and got stuck after writing the first loop. I wasn't quite sure how to continue with this code. I have no idea how to write more loops for the todo: portions of this code. Please help!
#include <cstdio>
#include <cstdlib>
usingnamespace std;
constint MAXFACTOR = 30;
// Factors array is to store the proper factors of the number under consideration.
// It is defined for 30 elements because, at most, the largest number 30 can have at most 30 factors
int factors [MAXFACTOR];
// This is the count of the actual number of proper factors.
int count;
// This is the sum of the proper factors
int sum;
int main() {
// Below is the loop that generates the candidate numbers
for (int number=2; number <= MAXFACTOR; number++) {
// print out the number under consideration
printf("Number: %d\n", number);
}
// since we have a new candidate zero out the factors of the previous number
// TODO: write a loop to zero out the factors array
for (int i=0; i<MAXFACTOR; i++) {
factors[i] = 0;
}
// now determine the factors of number. Start by setting the sum to zero.
count = 0;
// TODO: write another loop that tests possible factors using the modulus % operator
for (int i+1; i<number; i++) {
if (number % i == 0) {
factors[count] = i;
count++;
}
}
// if you find a proper factor store it in the factors array so it can be printed later
// at this point have found the factors of number
printf("Factors: ");
// TODO: print the factors of the number. Requires another loop.
printf("\n");
// if there is just one proper factor then the number is prime.
// TODO: print if the number is prime.
// compute the sum of the factors. Initialize the sum of factors to zero.
sum = 0;
// TODO: compute the sum of the PROPER factors
// if the sum==number the number is perfect
// TODO: test if the number is perfect. If it is print it.
}
system("pause");
return 0;
}