Skip to content

Instantly share code, notes, and snippets.

@madan712
Created January 28, 2014 07:13
Show Gist options
  • Save madan712/8663442 to your computer and use it in GitHub Desktop.
Save madan712/8663442 to your computer and use it in GitHub Desktop.
Java program to find prime numbers
public class PrintPrimeNumbers {
public static void main(String[] args) {
// the limit upto which you want to print the prime numbers
int limit = 200;
System.out.println("Printing prime numbers!");
for (int num = 2; num <= limit; num++) {
// print prime numbers only
if (isPrimeNumber(num)) {
System.out.println(num);
}
}
}
public static boolean isPrimeNumber(int num) {
for (int i = 2; i < num; i++) {
if (num % i == 0) {
return false; // if number is divisible then its not a prime number
}
}
return true; // if no divisible found then the number is prime number
}
}
@CodingBel
Copy link

// Hope this program is easy to understand and clear enough
// I wrote this program to check if whether a given number is prime or not.
// If the number is a prime number, the function returns 1 else it returns 0.

public class Is_NPrime
{
public static int IsPrime (int N)
{
int Dividers = 0;
int ReturnValue = 1;

	for (int i = 1; i < N; i++)
	{
		if (N % i == 0)
			Dividers ++; 
		
		if (Dividers > 1)     // Break from the loop as soon as possible!! 
		{
			ReturnValue = 0;
			break; 
		}
	}
	
	return ReturnValue; 
}

public static void main(String[] args) 
{
	System.out.println (IsPrime (23));
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment