Print 5 numbers per line
This program checks if a number is prime and if it isn't prime outputs the divisors
Lines 33-48 outputs the divisors and I want them to come out 5 per line.
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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
int num, count = 0, divisor;
string choice;
cout << "This program will determine if a number between 1 and 1000 is prime" << endl;
do
{
cout << "Please enter a number ";
cin >> num;
while (num < 1 || num > 1000)
{
cout << "Enter valid number ";
cin.clear();
cin.ignore(200, '\n');
cin >> num;
}
for (divisor = 1; divisor <= num; divisor++)
{
if (num % divisor == 0)
count++;
}
if (count == 2)
{
cout << num << " is prime" << endl;
}
else
cout << num << " is not prime" << endl;
while (count > 2)
{
cout << "Divisors: ";
divisor = 1;
while (divisor <= num)
{
if (num % divisor == 0)
{
cout << divisor << "\t";
}
divisor++;
}
count -= 16;
cout << "\n";
}
cout << "\n";
cout << "Would you like to check another number? (y or n) ";
cin >> choice;
while (choice != "y" && choice != "Y" && choice != "n" && choice != "N")
{
cout << "Please enter y for yes or n for no ";
cin.clear();
cin.ignore(200, '\n');
cin >> choice;
}
if (choice == "n" || choice == "N")
return (0);
} while (choice == "y" || choice == "Y");
}
|
int ctrl = 1;
...
cout << divisor etc...
if(ctrl%5 == 0)
cout << endl;
ctrl ++;
Thanks jonnin, I ended up formatting it like this and it works.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
while (count > 2)
{
cout << "Divisors: " << endl;
divisor = 1;
count2 = 1;
while (divisor <= num)
{
if (num % divisor == 0)
{
cout << divisor << "\t";
if (count2 % 5 == 0)
{
cout << endl;
count2 = 1;
}
else count2++;
}
divisor++;
}
count -= 16;
cout << "\n";
}
|
Last edited on
Topic archived. No new replies allowed.