Prime numbers with C++

A Prime number can only be divided by one and itself. For example; 13 can only be divided by 1 and 13. Here’s a C++ code that prints out the all prime numbers between 0 and 100.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
bool isPrime=true;
int num = 100;
 
for ( int i = 0; i <= num; i++)
{ 
	for ( int j = 2; j <= num; j++)
	{ 
		if ( i!=j && i % j == 0 )
		{ 
			isPrime=false;
			break;
		}
	}
	if (isPrime)
	{
		cout <<"Prime:"<< i << endl;
	}
 
	isPrime=true;
}
Share This!

Leave a Reply

Your email address will not be published.