Prime numbers: print 10 only per line

Hey guys, I'm fairly new to C++ and programming, one of my task is to print the first 100 prime numbers, with 10 of them per line with size 4. I got on really well with the finding of the prime numbers but how do you print 10 per line and change the size? Many thanks, here's what I got so far:

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
#include <iostream>
#include <math.h>
#include <iomanip>
#include <string>

using namespace std;

int IsPrimeNumber(int i);

int main()
{
	int i;
        int primesFound;
  
	i = 1;
	primesFound = 0;
	  
	cout << "The first 100 prime numbers are:" <<endl;
	
	while(primesFound < 100)
	{
		i++;
		if(IsPrimeNumber(i) == 1)
		{
			cout << i << " ";
			primesFound++;
		}
	 }
	
	string line;
   
	system("pause");
	return 0;
}

int IsPrimeNumber(int i)
  {
	int isPrime = 1;
	for(int j = 2; j <= (i/2); j++)
	{
		if((i%j) == 0) 
		{
			isPrime = 0;
		}
	}
	return isPrime;
  }
Last edited on
Between 23 and 27 line:
1
2
3
4
5
6
7
		if(IsPrimeNumber(i) == 1)
		{
			cout << i << " ";
			primesFound++;
                        if (primesFound % 10 == 0)
                            cout << endl;
		}
About size: you cannot change size of your text in terminal window. But you could use OS specific API to change font size of the terminal. NOTE: it will change size of ALL text. You cannot have two words of different font size on single terminal window simultaneously.
Also I slightly optimized your 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
#include <iostream>
#include <iomanip>

bool isPrimeNumber(int i)
{
    for(int j = 2; j <= (i/2); ++j) {
        if((i%j) == 0)
            return false;
    }
    return true;
}


int main()
{
    using namespace std;

    int i = 2; //1 isn't a prime number
    int primesFound = 0;

    cout << "The first 100 prime numbers are:" << endl;

    while(primesFound < 100) {
        if(isPrimeNumber(i)) {
            cout << setw(4) << i;
            ++primesFound;
            if (primesFound % 10 == 0)
                cout << endl;
        }
        ++i;
    }

    return 0;
}
guys thanks so much, really helpful. Cheers!
Topic archived. No new replies allowed.