The program is supposed to output 10 prime numbers per line, and it does. The problem is the spacing varies between the lines. Anyone know how to fix it?
#include <iostream>
usingnamespace std;
bool coutprime(int);
//--------------------------------------------------
int main() //cout prime numbers till 100
{
int line = 0; //10 per line
for (int n = 2; n <= 542; ++n)
{
if (coutprime(n))
{
cout <<" " << n;
line++;
}
//new line after 10
if (line % 10 == 0)
{
cout << "\n";
}
}
system("PAUSE");
}
bool coutprime(int n)//function
{
bool prime = true;
for (int c = 2; c <= n / 2; ++c)
{
if (n % c == 0)
{
prime = false;
break;
}
}
return prime;
}
#include <iostream>
#include <iomanip>
bool checkPrime(int);
//--------------------------------------------------
int main()
{
int line = 0; //10 per line
for (int n = 2; n <= 542; ++n)
{
std::cout << std::setw(5);
if (checkPrime(n))
{
std::cout << n;
line++;
//new line after 10
if (line % 10 == 0)
{
std::cout << '\n';
line = 0;
}
}
}
}
bool checkPrime(int n)//function
{
bool prime = true;
if (n < 2)
{
returnfalse;
}
for (int c = 2; c <= n / 2; ++c)
{
if (n % c == 0)
{
prime = false;
break;
}
}
return prime;
}