473,324 Members | 2,166 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

How do you find the sum of the factors of a number?

I have an assignment for my high-school level programming class, and I'm basically unable to figure out how to add the sums of the factors.
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
  1. //Purpose: Displays the sum of the factors
  2.  
  3. #include<iostream.h> //Necessary for cin and cout commands
  4. main() //Tells compiler this is a function
  5. {
  6. //Declares variables
  7. int n; //Number user inputs
  8. int f; //Factors
  9. int num; //Number that either continues or ends the do while loop
  10.  
  11. do //Uses a do while loop to loop and end the program
  12. {
  13. //Asks user to enter a positive integer
  14. cout << "Enter a positive integer: ";
  15. cin >> n; //Stores this in variable n
  16. //Outputs the sum of the factors of n
  17. cout << "The sum of the factors of " << n << " are:\n";
  18.   for(f = 1; f <= n; f++) //Divides n until f = n
  19.   { 
  20.     if ( n % f == 0) //When n / f has no remainders, f is a factor of n
  21.     { cout << f << '\n'; }
  22.   } //Ends the for loop
  23.  
  24. cout << "Enter any number to continue the program.\n";
  25. cout << "Enter 0 to end the program: ";
  26. cin >> num;
  27. } //Ends the do loop
  28.  
  29. while(num!=0);
  30. return 0;
  31. } //Ends the program
Any help I can get with this will be extremely appreciated! As you can see, I have a few problems using loops. >>;;

Again, I'm looking for a loop or other piece of code that would add the factors of the input (n). Thanks!! :D

-Shiranami
Jul 25 '07 #1
1 9625
gpraghuram
1,275 Expert 1GB
I have an assignment for my high-school level programming class, and I'm basically unable to figure out how to add the sums of the factors.
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
  1. //Purpose: Displays the sum of the factors
  2.  
  3. #include<iostream.h> //Necessary for cin and cout commands
  4. main() //Tells compiler this is a function
  5. {
  6. //Declares variables
  7. int n; //Number user inputs
  8. int f; //Factors
  9. int num; //Number that either continues or ends the do while loop
  10. int factor_sum=0; //Added by me .
  11.  
  12. do //Uses a do while loop to loop and end the program
  13. {
  14. //Asks user to enter a positive integer
  15. cout << "Enter a positive integer: ";
  16. cin >> n; //Stores this in variable n
  17. //Outputs the sum of the factors of n
  18. cout << "The sum of the factors of " << n << " are:\n";
  19.   for(f = 1; f <= n; f++) //Divides n until f = n
  20.   { 
  21.     if ( n % f == 0) //When n / f has no remainders, f is a factor of n
  22.     { cout << f << '\n';
  23.       factor_sum += f; //Added by me
  24.     }
  25.   } //Ends the for loop
  26.  
  27. cout << "Enter any number to continue the program.\n";
  28. cout << "Enter 0 to end the program: ";
  29. cin >> num;
  30. } //Ends the do loop
  31.  
  32. while(num!=0);
  33. return 0;
  34. } //Ends the program
Any help I can get with this will be extremely appreciated! As you can see, I have a few problems using loops. >>;;

Again, I'm looking for a loop or other piece of code that would add the factors of the input (n). Thanks!! :D

-Shiranami
I have added 2 lnes in your code an i think it will solve the purpose.
The code which i added have the comment //added by me.


Raghuram
Jul 25 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Jenny Zhang | last post by:
The osdl-dbt3 test starts with building and vacuuming the database. The execution plans were taken after the vacuuming. I did two tests with the same database parameters: 1. run two osdl-dbt3...
7
by: Freyr | last post by:
Hello, I'm taking an independant course in C++, and one of my questions asks to use the following algorithm to deturmine the factors of any given number: -- Initialize a counter at 2 So Long...
21
by: Imran | last post by:
I have a vector of integers, such as and I want to find out the number which occurs most frequently.what is the quick method. My array size is huge. what I am doing is 1. find out the...
0
by: rm | last post by:
Delineating Factors for Code Placement I am not a fan of business rule rich stored procedures. However I am against the grain on that thought in the organization that I work for. What...
7
by: Paulo Matos | last post by:
Hi all, Given a float type, is there a way to find the rational which is equal to it? I can imagine such a process to work in theory but I don't know if this will work in C++. I'd like comments...
2
by: vvijayan | last post by:
I want to know what is the size of an object and what are the factors that decide it
3
by: rcarwise | last post by:
Iam having trouble getting started on this program and wanted to know if I could get help on writing a method and loops to get started. this is the program that I have to do: Write a...
3
by: jimix | last post by:
I have written the program for factors of #'s 1-50 but the factors print out below the number being factored. I can't figure out how to make it all on the same line. i.e. (now) divisors of 25 are...
15
by: John A Grandy | last post by:
Does .NET provide a "factors of" method ? I need to determine the factors of an integer.
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.