You can do more than just print a number, if it is a factor. You can increase a counter by one too. The counter must obviously be 0 before the loop starts.
PS. Please, use the code tags in the posts. (You can even edit your older post to add them.)
If the number you want to get the factors from, can be evenly divided (without a rest), its a factor. A number can have multiple factors, as keskiverto already said, thats why you should create a for Loop and increase the number you divide through continuously...
I wouldn't do it with a while Loop...
Something like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
int NumberIWantAllTheFactorsFrom;
std::cout << "Enter a number you would like to get all factors from: ";
std::cin >> NumberIWantAllTheFactorsFrom;
int FactorCounter = 0;
for ( int i = 1; i <= NumberIWantAllTheFactorsFrom; i++)
{
if ( NumberIWantAllTheFactorsFrom % i == 0 ) //if theres no rest
{
std::cout << i << ", "; //Creates a list in console Output
FactorCounter++; // or FactorCounter += 1, whatever you like better
}
}
std::cout << "There's a total of " << FactorCounter << " Factors for the number " << NumberIWantAllTheFactorsFrom;
int num = 0;
int x = 1;
std::cout << "Type a number: ";
std::cin >> num;
std::cout << "The factors of " << num << " are ";
int FactorCounter = 0; //Declare the Counter, starting at 0 ofc
while(x <= num)
{
if(num % x == 0)
{
std::cout << x << ", ";
FactorCounter++; //If theres no rest, add one to the counter
}
x++;
}
std::cout << std::endl;
std::cout << "There are " << FactorCounter << " factors";
return 0;
}