Hello all,

I had previously written a short program to find the factors of any integer. Fortunately, it works flawless and without any problems. However, I want to edit this code so that the program adds up all the factors of that certain integer. Can anyone shed some light as to how to do this?

The code I have is the following

``````#include <iostream>
using namespace std;

main()
{

int a;
int b;

b = 0;

cout<<"Enter a number:";
cin>>a;

for (b=2; b<=a; b++)

{
if (a % b == 0)
cout << b << " is a factor of " << a << endl;
}

return 0;
}``````

I tried stating that

``````sum = 1
and that sum= sum + b + a / b``````

But it didn't work.

Help would very much be appreciated.

4
Contributors
11
Replies
12
Views
9 Years
Discussion Span
Last Post by u8sand

If you have a number to add use this `sum=sum+a;` or `sum+=a;` Both do the same thing of adding the value a to the value sum.

you will need to expand you if to only add to sum if a is a factor
you can use this construct

``````if (condition)
{
// statement 1
// statement  2
//...
}``````

Note the braces allow multiple statements if the condition is true

Lastly, thanks for reading the forum rules before posting and feel free to post your next version, when/if you get stuck.

The problem isn't with adding a specific number, but adding ALL the factors of an integer.

For example:

The factors of 36 are : 2, 3 , 4, 6, 9, 12, 18, 36

I'm looking to add these factors so that I can display:

``cout << "The sum of the factors is" << unknown << endl ;``

Which in this case, would be: 2+3+4+6+9+12+18+36 = 90

I think this is what you want. Add a temp and use it to add together your factors.

``````#include <iostream>
using namespace std;

main()
{

int a;
int b;
int temp = 0;

b = 0;

cout<<"Enter a number:";
cin>>a;

for (b=2; b<=a; b++)

{
if (a % b == 0)
cout << b << " is a factor of " << a << endl;
temp = temp + b;
}
cout << "The sum of the factors is " << temp;

return 0;
}``````

Sorry code tag didn't work.

Not a problem, lets consider the following code:

``````int sum=0;
for(int i=0;i<=10;i++)
{
sum+=i;
}
std::cout<<"Sum == "<<sum<<std::endl;``````

Now what you have there is the sum of the fiirst 10 numbers (55), you add up the even numbers upto 10.

``````int sum=0;
for(int i=0;i<=10;i++)
{
if (i % 2 )
{ sum+=i; }
}
std::cout<<"Sum (even)== "<<sum<<std::endl;``````

That is very very close to the code that you are trying to write. It has a loop, and a condition that must be met. note that the initialization of sum is outside of the loop, so is the printing of the result.

Thanks for your efforts guys, I really do appreciate them. However, neither of the codes displays a correct sum.

Stu, your first code always displays 55 as the sum (no matter what integer I insert), and the second one always displays 25 as the sum.

MatEpp, I tried yours but the sum for the factors of 10 (which is 8) showed up to be a three-figure number.

I've tried a couple of my own methods, but no success yet. Again...let's say the number I insert is 10.

The proper divisors of 10 add up to 8. (1+2+5). Eight, would be the number that I'm looking to output with my program.

Take my code and look at the loop. It is a test code so you can get to understand loops.

Change the 10 to 11 then recompile and run. Then change the 11 to 12 and recompile and run. Now do you see how it works?

Oh I see what's going on. Just study the loop like StuXYZ is saying. And I wrote bad code earlier. Here it is corrected.

``````if (a % b == 0)
{
cout << b << " is a factor of " << a << endl;
temp = temp + b;
}
cout << "The sum of the factors is " << temp;``````

And the concept behind this is to create an integer that is zero. Then you have your factors as b.
So say b = 1, then 1 + 0 = 1. temp = 1
Then say b equals 2 = 1 + 2 =3. temp = 3
And so on until the loop stops.

Hope that helps.

Hi guys!

Almost got it, just need a little help with a last step.

My current code is:

``````#include <iostream>
using namespace std;

main()
{

int a;
int b;
int sum;

b = 0;
sum = 1;

cout<<"Enter a number:";
cin>>a;

for(b=2;b<=a;b++)
{
if (a % b ==0 )
{ sum+=b; }
}
std::cout<<"Sum == "<<sum<<std::endl;

return 0;
}``````

This works very well. However, I am having a problem. Let me illustrate it through an example.

The factors of 36 are: 1+2+3+4+6+9+12+18...which equal up to 55.

However, the program displays that the sum is 91, because it includes 36 as a factor. 1+2+3+4+6+9+12+18+36 = 91.

I don't want to include 36 as a factor. How do I take it out?

Thanks again for all your help, you guys have been great.

Disregard the problem on my previous post. I fixed it with a quick change to my sum equation.

Now I have YET another problem. The adding of the factors of the first number FINALLY works perfectly. However, when I try to do it with a second number, it doesn't work.

The sum is always incorrect (seems like somehow it's related to the first number I input, but I can't figure out what it is). Can someone review my code and check where I might be going wrong ( I know for a fact that it's on the bottom half of the code, after asking the user to input a second number). Again, the addition of the factors of the 1st number comes out alright, but the addition of the factors of the 2nd number is always incorrect. I thought it was just a matter of recycling code?

``````#include <iostream>
using namespace std;

main()
{

int a;
int b;
int c;
int d;
int sum1;
int sum2;

//FIRST NUMBER
cout<<"Enter First number:";
cin>>a;

sum1=1-a;

for(b=2;b<=a;b++)
{
if (a % b ==0 )
{ sum1+=b; }
}

cout<<"Sum== "<<sum1<< endl;
cout<<endl;

//SECOND NUMBER

cout<<"Enter second number:";
cin>>c;

sum2=1-c;

for(d=2;d<=c;d++)
{
if (c % d ==0 )
{ sum1+=d; }
}

cout<<"Sum2=="<<sum2<<endl;
cout<<endl;

return 0;
}``````
``````#include <iostream>
using namespace std;

main()
{

int a;
int b;
int c;
int d;
int sum1;
int sum2;

//FIRST NUMBER
cout<<"Enter First number:";
cin>>a;

sum1=0-a;

for(b=2;b<=a;b++)
{
if (a % b ==0 )
{ sum1+=b; }
}

cout<<"Sum== "<<sum1<< endl;
cout<<endl;

//SECOND NUMBER

cout<<"Enter second number:";
cin>>c;

sum2=1-c;

for(d=2;d<=c;d++)
{
if (c % d ==0 )
{ sum1+=d; }
}

cout<<"Sum2=="<<sum2<<endl;
cout<<endl;

return 0;
}``````

try that?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.