I need to write a program that when the user enters a positive integer it out puts the number of factors that number has. Such as if I enter 24 the program outputs 8.
Remember how to do it on paper. Start with two rows:
24
1
Now just add one to the last bottom number. Every time you get two new factors, write them top and bottom (largest on the top, smallest on the bottom).
Repeat until the number is not already among the numbers on the top.
24 12 8 6
1 2 3 4 (the next would be 6, which is already on the top)
Remember, it is possible for the last pair to be the same number, and we don't count the same number as two separate factors.
25 5
1 5
I need it to tell me how many outputs it has instead of giving me them. This is what I have now.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
#include <cstdlib>
usingnamespace std;
int main ( )
{
int N;
cout << "Please enter a positive integer value ";
cin >> N;
for ( int i = 1; i <= N; ++i ) // note that 1 and num are both trivially divisors of num
{
if ( N % i == 0 ) // only check for divisibility
{
std::cout << i << std::endl;
}
}
}
This is what I'm getting now:
if I enter 24, I get back this.
1
2
3
4
6
8
12
24
I need it to just tell me that there's 8 factors. someone help point me in that direction please!