Prime Numbers in Java.

A prime number is a positive integer that has exactly two factors, 1 and the number itself. We know 0 is neither a positive nor a negative number. Otherwise, 0 is a neutral number. So, it is not a prime number, and 1 is not a prime number, 1 has only one factor, that’s 1.

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)
	{
		System.out.println("Prime: "+i);
	}
 
	isPrime=true;
}
Share This!

Leave a Reply

Your email address will not be published.