I am currently trying to generate a program using a double nested for loop to get the following output:
>Please enter the maximum integer value you wish to find divisors: 10
>List of divisors of the integer between 1 and 10:
>divisors of 1: 1
>divisors of 2: 1, 2
>divisors of 3: 1, 3
>divisors of 4: 1, 2, 4
>divisors of 5: 1, 5
>divisors of 6: 1, 2, 3, 6
>divisors of 7: 1, 7
>divisors of 8: 1, 2, 4, 8
>divisors of 9: 1, 3, 9
>divisors of 10: 1, 2, 5, 10

Now I believe I have implemented the right arithmetic but I can not seem to get the correct format for the output and I am not sure where to go from here. This is my code and the results I am getting from it.

/*

 */
#include <cmath>
#include <iostream>
using namespace std;

int main()
{
	int num, countA, countB;
	
	cout << "Please enter a positive maxiumum integer value you wish to find divisors: \n";
	cin >> num;
	if (num <= 0)
	{
	cout << "The input entered was invalid. Please input a positive whole integer.\n";
	}
	else
	{
	
	for(countA = 1; countA <= num; countA++)
		{
		
			for(countB = 1; countB<= num; countB++)
			{
			
			 if(countB % countA == 0)
			{			
		
			cout << "Divisors of " << countB << ": " << countA << endl;
		
			}
			
			}
		}
	}
return 0;
}

Results:
Please enter a positive maxiumum integer value you wish to find divisors:
10
Divisors of 1: 1
Divisors of 2: 1
Divisors of 3: 1
Divisors of 4: 1
Divisors of 5: 1
Divisors of 6: 1
Divisors of 7: 1
Divisors of 8: 1
Divisors of 9: 1
Divisors of 10: 1
Divisors of 2: 2
Divisors of 4: 2
Divisors of 6: 2
Divisors of 8: 2
Divisors of 10: 2
Divisors of 3: 3
Divisors of 6: 3
Divisors of 9: 3
Divisors of 4: 4
Divisors of 8: 4
Divisors of 5: 5
Divisors of 10: 5
Divisors of 6: 6
Divisors of 7: 7
Divisors of 8: 8
Divisors of 9: 9
Divisors of 10: 10

See how the outputs are vertical and aren't organized into a nice table? I was hoping someone could point me in the right direction? It would be greatly appreciated!

Recommended Answers

All 6 Replies

#include <cmath>
#include <iostream>
using namespace std;

int main()
{
	int num, countA, countB;
	
	cout << "Please enter a positive maxiumum integer value you wish to find divisors: \n";
	cin >> num;
	if (num <= 0)
	{
		cout << "The input entered was invalid. Please input a positive whole integer.\n";
	}
	else
	{
	
		for(countA = 1; countA <= num; countA++)
		{
		
			cout << "Divisors of " << countA << ": ";
			for(countB = 1; countB<= num; countB++)
			{ 
				if(countA % countB == 0)
				{			
					cout << countB << " " ;
				}			
			}
			cout << endl;
		}
	}
	return 0;
}

Enjoy !!!

normally we try to help people here an not do the work for them but yea.... ok

normally we try to help people here an not do the work for them but yea.... ok

Well that's true and you are right. But you did notice that he had done all the work and it would be too wasteful to explain things like put a 'cout' here and then put and 'endl' there. It is just to trivial and sometimes, when the solution is to trivial, code explains more than words. You see that happening a lot of times. But again, it is important to realize when to do that.

sorry but I attempted doing what you told me and I'm still not getting the organized table I'm looking for as an output even though it is still computing the correct values

What does your output look like now? That will help steer you in the right direction.

Ask yourself "What statement can I move where to make this look right?" Or possibly, "What extra statement do I need to add to make this look different?"

sorry but I attempted doing what you told me and I'm still not getting the organized table I'm looking for as an output even though it is still computing the correct values

Did you try to do a diff between the new and old code and find what was different? I did not put any explanation in there so that you would do that and try and see what was changed.

This is the output that I'm getting, by just copy pasting the code and running it

Please enter a positive maximum integer value you wish to find divisors:
10
Divisors of 1: 1
Divisors of 2: 1 2
Divisors of 3: 1 3
Divisors of 4: 1 2 4
Divisors of 5: 1 5
Divisors of 6: 1 2 3 6
Divisors of 7: 1 7
Divisors of 8: 1 2 4 8
Divisors of 9: 1 3 9
Divisors of 10: 1 2 5 10

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.