basically this is my assignment:
Write a program to determine and display the divisors of a number n and their count.
The program will do the following:
1. asks the user to enter the value of n;
2. determine and display the divisors of the number n;
3. count and display the divisors of the number n.
Example
n = 15
divisors: 1, 3, 5, 15
15 has 4 divisor
currently i have this code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
int num, countA, countB;
cout << "Please enter a positive number you wish to find the divisors of: \n";
cin >> num;
if (num <= 0)
{
cout << "The number you entered was invalid. Please run the program again.\n";
}
else
{
for(countA = 1; countA <= num; countA++)
{
cout << "The Divisors of " << countA << ": ";
for(countB = 1; countB<= num; countB++)
{
if(countA % countB == 0)
{
cout << countB << " " ;
}
}
cout << endl;
}
}
return 0;
}
|
this is more than i need, what i need help with is what i need to do so it just shows the divisors of the number i entered, and not all of the numbers before this, at the moment if i enter 20, it will show the divisors for 1, 2, 3, 4 etc...all i want it to do is to shows the divisors of 20.
thanks.