displaying commas in loop
If the number 4 is entered, the output should be the following:
1: 1
2: 1, 2
3: 1, 3
4: 1, 2, 4,
My output, however, is different but I know its an error with the comma.f
1: 1, , ,
2: 1, 2, ,
3: 1, , 3,
4: 1, 2, , 4
Any help with this little comma problem is greatly appreciated!
Here is my 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 34 35 36 37 38 39 40 41
|
#include <iostream>
using namespace std;
int main()
{
int n(0), j(0);
int i(0);
cout << "Enter integer: ";
cin >> n;
while (n < 1)
{
cout << "Number must be positive." << endl;
cout << "Enter integer: ";
cin >> n;
}
cout << endl;
cout << "Factors of all numbers up to " << n << endl;
for (i = 1; i <= n; i++)
{
cout << i << ": ";
for (j = 1; j <= n; j++)
{
if (i % j == 0)
{
cout << j;
}
if (j != i)
{
cout << ", ";
}
}
cout << endl;
}
}
|
Please don't double post
Topic archived. No new replies allowed.