Prime Number Finder In Java

A prime number (or a prime) is a natural number which has exactly two distinct natural number divisors: 1 and itself.
This small program runs through 2-500 and print all the prime numbers in that range.

Note: The number 1 is by definition not a prime number.

package com.kushal.utils;

public class PrimeNumberInJava {
	/**
	 * Method to test whether a number is
	 * a prime number or not
	 */
	public static boolean isPrimeNumber(int number) {
		boolean prime = true;
		int limit = (int) Math.sqrt(number);

		for (int i = 2; i <= limit; i++) {
			if (number % i == 0) {
				prime = false;
				break;
			}
		}

		return prime;
	}

	public static void main(String[] args) {

		/**
		 * Loop From 2-500 and Print all the prime numbers
		 */
		System.out.println("List Of Prime Numbers From 2-500");
		for (int i = 2; i <= 500; i++) {
			if (isPrimeNumber(i))
			{
				System.out.println(i);
			}
		}
	}
}

———————————
Here is the output of this program:

List Of Prime Numbers From 2-500
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
101
103
107
109
113
127
131
137
139
149
151
157
163
167
173
179
181
191
193
197
199
211
223
227
229
233
239
241
251
257
263
269
271
277
281
283
293
307
311
313
317
331
337
347
349
353
359
367
373
379
383
389
397
401
409
419
421
431
433
439
443
449
457
461
463
467
479
487
491
499

  1. How to extract plain Text from HTML Website easily in Java
  2. Recursively Finding Greatest Common Divisor (GCD) – Java Implementation
  3. In Place Matrix (2D Array) Clockwise and Counterclockwise Rotation – Java Implementation
  4. Matrix (2D Array) Clockwise and Counterclockwise Rotation with Extra Buffer – Java Implementation
  5. How To Find What Java Version You Are Using?
  6. Computing the total, free and usable disk space easily using JDK 1.6
  7. Creating java.util.date and java.util.sql objects from Calendar object
  8. Bar Code Generation In Java Using iText API
  9. Simple Tag Generator In Java For Music Blogs
  10. Tutorial on Converting an List of Strings or Numbers to a CSV file with optional sorting
  11. Calling URL Browser From Java Application
  12. Java Reflection Tutorial – List Methods Of A Class
  13. Cleaning Punctuation And White Spaces From A String
  14. Java Tutorial – Using JCIFS to copy files to shared network drive using username and password
  15. Prime Number Finder In Java
  16. Utility Class For Getting SQL Connection and Closing It
  17. Java Design Pattern – Singleton Pattern
  18. Reading / Writing File in Java and String Manipulation
  19. How to capture a screenshot using Java and save it to a file?
  20. Calculating The Difference Between Two Dates In Terms of Months
  21. Getting The Name Of Currently Logged In User In The System
  22. How To Set And Get System Clipboard Contents In Java
  23. RGB To Hex and Hex To RGB Conversion Using Java and Regular Expressions
  24. Generating Nepali Unicode Sequence For Characters
  25. Java Tool To Compare Two Lists and Spit Out The Differences – Working Example
  26. Replacing non-ASCII characters using Java Regular Expressions
  27. Calculating Folder Size In Java
  28. Printing Fibonacci Sequence Using Recursive and Iterative Methods
  29. Finding Square Root Of A Double Number In Java Using Binary Search
  30. How to swap two variables without using extra temporary variable?
  31. Rotating a two dimensional integer array In-Place and using extra memory
  32. Finding Mean Value Of An Integer Array In Java
Tagged , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *