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!