help with divisors

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.
well it shows that because countA % countB will never be 0. and line 21 is printing the value of countA which increases in value by one. so the second for loop is useless and the first is just printing countA + 1
Your algorithm just doesn't make sense. You're supposed to be checking if the numers [0,num] can divide num by 0 (num%div == 0). Instead, your looping over all numbers [0,num] and checking if they can be divided by all the numbers [0,num].

Just think about it. Why on earth would you need two nested loops to check if [0,num] divdes num?
im a total beginner to C++ so not much makes sense to me anyway, iv sorted most of it out, now all i need to do is calculate how many divisors the number has, so 15 has 4 divisors, but how can i calculate that it has 4?

this is my code now:

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
34
35
#include <iostream> 
using namespace std;   
int main()
  
{
     int a,b;
     
     cout << "Enter a number you wish to find the divisors of: \n";
     
     cin >> a;
     b=1;
     while (b<=a) 
 
     {if(a%b==0) cout << a << " / " 

     << b << " = " << a/b << "\n";

    b++;}

    cout << "The divisors of " << a << " are: ";
         for(b = 1; b<= a; b++)
         { 
		if(a % b == 0)
		{			
			cout << b << " " ;
		}			
          }
          cout << endl;
    
 //   cout << "The number " << a << " has " << a << " divisors \n";

system("pause");
return 0;

}


the commented bit is the bit im stuck on now.
Last edited on
Why are you looping through it twice? Doesn't the first loop already show the divisors?

Anyway, keep a counter:
1
2
3
4
5
6
7
8
int counter(0);
//...
if (a%b == 0) { 
    //...
    counter++;
}
//...
cout << a << " has " << counter << " divisors!\n";
where would i put the counter? like i say in a noob at C++
There's a difference between being a newbie and simply being lazy...

What are you counting? Where do you find them? That's where your counter goes.
k, thanks for your help, its appreciated.
I'd put it here:
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
#include <iostream> 
using namespace std;   
int main()
  
{
     int a,b, counter=0;
     
     cout << "Enter a number you wish to find the divisors of: \n";
     
     cin >> a;

    cout << "The divisors of " << a << " are: ";
         for(b = 1; b<= a; b++)
         { 
		if(a % b == 0)
		{			
			cout << b << " " ;
			counter++;
		}			
          }
          cout << endl;
    
    cout << "The number " << a << " has " << counter << " divisors \n";

system("pause");
return 0;

}
Topic archived. No new replies allowed.