Below is my temporary program that only displays the factors, but I'm not sure which loop structure I should use to add them together.
(Erm, many comments are required for my teacher, so I apologize if they get in the way of you helping me.. ^-^;; )
Expand|Select|Wrap|Line Numbers
- //Purpose: Displays the sum of the factors
- #include<iostream.h> //Necessary for cin and cout commands
- main() //Tells compiler this is a function
- {
- //Declares variables
- int n; //Number user inputs
- int f; //Factors
- int num; //Number that either continues or ends the do while loop
- do //Uses a do while loop to loop and end the program
- {
- //Asks user to enter a positive integer
- cout << "Enter a positive integer: ";
- cin >> n; //Stores this in variable n
- //Outputs the sum of the factors of n
- cout << "The sum of the factors of " << n << " are:\n";
- for(f = 1; f <= n; f++) //Divides n until f = n
- {
- if ( n % f == 0) //When n / f has no remainders, f is a factor of n
- { cout << f << '\n'; }
- } //Ends the for loop
- cout << "Enter any number to continue the program.\n";
- cout << "Enter 0 to end the program: ";
- cin >> num;
- } //Ends the do loop
- while(num!=0);
- return 0;
- } //Ends the program
Again, I'm looking for a loop or other piece of code that would add the factors of the input (n). Thanks!! :D
-Shiranami