I may not have explained this very well in the title simply because I don’t really know how to explain it in the length a title has so I apologize for that. Anyways this is my current code:
// Gets the product that is used to get the factors
cout << "Enter a number that you want to be factored" << endl;
cin >> y;
system("cls");
// Gets the sum from the user that the two factors need to add up to
cout << "Input the sum that is needed" << endl;
cin >> neededSum;
secondary = y;
system("cls");
// Lists the factors that add up to the specified sum
cout << "The numbers that add up to " << neededSum << " are:" << endl;
for (int i = 0; i < 100; i++)
{
x++;
increment++;
y = secondary / x;
product = x * y;
if (product == secondary)
{
sum = x + y;
if (sum == neededSum)
{
cout << x << " and " << y << endl;
}
}
}
Basically it takes a product lists all the factors of that product. Then, it adds all of the factors together until it finds the one that adds up to the specified sum.
However, if the specified sum is negative like ‘-11′ for instance it won’t work because the factors of ’28’ (an example) are 4 and 7 not -4 and -7. I need to figure out a way to fix this so it knows when it has to be -4 and -7 instead of the positive reverse.
Thanks in advance for the help.
Just pretend that x and y are negative: