#include <iostream>
using namespace std;
int main()
{
int a;
int b;
int sum;
b = 0;
sum = 1;
cout<<"Enter integer, a:";
cin>>a;
for(b=2;b<=a;b++)
{
if (a % b ==0 )
{ sum+=b; }
}
cout<< "Sum of all factors of a equals= "<< sum <<endl;
return 0;
}
I am trying to write a code that will add up all divisors of any number, i have created this code but am stuck on how to make it work for 0 and negative numbers, not sure where to add to the code.

New Topic/Question
Reply



MultiQuote



|