• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer
  • Home
  • Java Tutorial
  • Java Posts
  • Node.js
  • Spring Core
  • Algorithms
  • Docker
  • Blogging
  • Misc
Tech Stack Journal

Tech Stack Journal

Print Prime Numbers in Java

September 10, 2019 by Admin Leave a Comment

In this post we will see couple of examples on how to print prime numbers in Java. A positive integer is called Prime Number when it is greater than 1 and divisible only with itself and by 1. Example: 2, 3, 5, 7, 11, 13, 17, 19 etc.,

Print Prime Numbers using custom square root function:

package com.techstackjournal;
/**
 * 
 * Program to print prime numbers within a given limit
 *
 */
public class PrimeNumbersMethod1 {
	public static void main(String args[]) {
		int limit = 200;
		for (int n = 2; n <= limit; n++) {
			if (isPrimeNumber(n)) {
				System.out.format("%d ",n);
			}
		}
	}
	/**
	 * Checks if given number is Prime or not
	 * 
	 * @param n
	 * @return
	 */
	private static boolean isPrimeNumber(int n) {
		for (int i = 2; i <= squareRoot(n); i++) {
			if (n % i == 0) {
				return false;
			}
		}
		return true;
	}
	/**
	 * Returns the square root of a number
	 * 
	 * @param num
	 * @return
	 */
	private static double squareRoot(double num) {
		double n2 = num / 2;
		double n1;
		do {
			n1 = n2;
			n2 = (n1 + num / n1) / 2;
		} while (n1 - n2 != 0);
		return n2;
	}
}

Prints Prime Numbers using Math class

package com.techstackjournal.java.basics;
/**
 * 
 * Program to print prime numbers within a given limit
 *
 */
public class PrimeNumbersMethod2 {
	public static void main(String args[]) {
		int limit = 200;
		for (int n = 2; n <= limit; n++) {
			if (isPrimeNumber(n)) {
				System.out.format("%d ", n);
			}
		}
	}
	/**
	 * Checks if given number is Prime or not
	 * 
	 * @param n
	 * @return
	 */
	private static boolean isPrimeNumber(int n) {
		for (int i = 2; i <= Math.sqrt(n); i++) {
			if (n % i == 0) {
				return false;
			}
		}
		return true;
	}
}

Filed Under: Java

Previous Post: « Finding Square Root of a Number in Java
Next Post: Piping Readable Stream to Writable Stream »

Reader Interactions

Leave a Reply Cancel reply

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


Primary Sidebar

More to See

Arrays.asList in Java Examples

February 21, 2021 By Admin

[Solved] Why List.add throws UnsupportedOperationException in Java?

February 20, 2021 By Admin

Secondary Sidebar

Categories

  • Algorithms
  • Blogging
  • Docker
  • Java
  • Misc
  • Node.js
  • Spring Core
  • Windows

Archives

  • February 2021 (6)
  • January 2021 (1)
  • December 2020 (1)
  • September 2020 (2)
  • August 2020 (5)
  • July 2020 (4)
  • June 2020 (1)
  • May 2020 (4)
  • April 2020 (22)
  • November 2019 (3)
  • September 2019 (2)
  • August 2019 (6)

Footer

Navigation

  • Home
  • Java Tutorial
  • Java Posts
  • Node.js
  • Spring Core
  • Algorithms
  • Docker
  • Blogging
  • Misc

Recent

  • How to Make File Explorer Open to This PC instead of Quick Access in Windows 10
  • Arrays.asList in Java Examples
  • [Solved] Why List.add throws UnsupportedOperationException in Java?
  • How to Convert an Array to List in Java?
  • How Many Spaces in a Tab?

Search

Copyright © 2021 · Tech Stack Journal · Log in