Prime Number

A natural number (i.e. 1, 2, 3, 4, 5, 6, etc.) is called a prime or a prime number if it is greater than 1 and has exactly two divisors, 1 and the number itself.Natural numbers greater than 1 that are not prime are called composite. ~ wikipedia

package org.paingan.labs.logic;

public class PrimeNumber {
public static void main(String[] args) {
int limit = 10;

// loop through the numbers one by one
for (int i = 1; i < limit; i++) {

boolean isPrime = true;

// check to see if the number is prime
for (int j = 2; j < i; j++) {

if (i % j == 0) {
isPrime = false;
break;
}
}
// print the number
if (isPrime) System.out.print(i + " ");
}
}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s