473,976 Members | 2,326 Online

# finding divisors of a all numbers less than a given num

2 New Member
Hi,
I have to find the divisors of all numbers less than a number inputed by the user and state if a number is prime if no divisors. I have this code so far, but I'm getting the wrong output and not sure how to list all the numbers less than the input number. Please help.

#include <iostream>
using namespace std;

// function declarations
// -------------------
bool isPrime (int);

// main function
// -------------------

int main ()
{
int userNum, divisor;
bool prime;
cout << "Please enter an integer between 10 and 100:";
cin >> userNum;
if(userNum < 10 || userNum > 100)
{
cout << "Please enter an integer between 10 and 100:";
}
else

for (int counter = 2; userNum > counter; counter ++)
{
if (userNum % counter == 0)
{
cout << userNum << "'s divisors are:" << counter;
}
}

isPrime(userNum );
prime = isPrime(userNum );
if (prime == 1)
{
cout << userNum << " is a prime number" << endl;
}

return 0;
}

// function definitions
// -------------------

bool isPrime(int userNum)
{
for(int counter = 2; counter < userNum; counter++)
{
if(userNum % counter ==0)
return false;
}
return true;
}
Feb 9 '07 #1
2 2024
jknox13
2 New Member
ok I have the programming right, I just need to fix the output to look pretty like
16's divisors are: x, y z
15's divisors are:....
and so on.
Expand|Select|Wrap|Line Numbers
1. #include <iostream>
2. using namespace std;
3.
4. // function declarations
5. // -------------------
6. bool  isPrime (int);
7.
8.
9.
10. // main function
11. // -------------------
12.
13. int main ()
14. {
15.     int userNum;
16.     bool prime;
17.     cout << "Please enter an integer between 10 and 100:";
18.     cin >> userNum;
19.     if(userNum < 10 || userNum > 100)
20.     {
21.         cout << "Please enter an integer between 10 and 100:";
22.     }
23.     else
24.     while (userNum >= 1)
25.     {
26.      userNum--;
27.      for (int counter = 2; userNum > counter; counter++)
28.         {
29.             if (userNum % counter == 0)
30.                 {
31.                     cout << userNum << "'s divisors are:" << counter << endl;
32.                 }
33.             else
34.             isPrime(userNum);
35.             prime = isPrime(userNum);
36.             if (prime == 1)
37.             {
38.                 cout << userNum << " is a prime number" << endl;
39.             }
40.         }
41.     }
42.
43.
44.
45.
46.
47.
48.
49.     return 0;
50.     }
51.
52. // function definitions
53. // -------------------
54.
55.
56. bool isPrime(int userNum)
57. {
58.     for(int counter = 2; counter < userNum; counter++)
59.     {
60.         if(userNum % counter ==0)
61.             return false;
62.     }
63.     return true;
64. }
Feb 9 '07 #2
Ganon11
3,652 Recognized Expert Specialist
At the beginning of the for... loop, outpu8t a statement like "The number xxx ", but without an endl or newline character. Next, check if it is prime. If it is prime, print out "is prime." and endl - then move to the next number. If the number isn't prime, then proceed with your calculations to find divisors. Every time you find one, print that divisor out - but without an endl. When you have found every divisor, print a newline, and move on to the next number.
Feb 10 '07 #3