Factors Code

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!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <cstdio>
#include <cstdlib>
using namespace std;



const int 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;
}
Topic archived. No new replies allowed.