Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Java Program to display a prime number less than the given number

public class Demo {
	public static void main(String[] args) {
		int val = 20;
		boolean[] isprime = new boolean[val + 1];
		for (int i = 0; i<= val; i++)
			isprime[i] = true;
			// 0 and 1 is not prime
			isprime[0] =  false;
			isprime[1] =  false;
			int n = (int) Math.ceil(Math.sqrt(val));
			for (int i = 0; i<= n; i++) {
				if (isprime[i])
					for (int j = 2 * i; j<= val; j = j + i)
						// not prime
						isprime[j] = false;
			}
			int myPrime;
			for (myPrime = val; !isprime[myPrime]; myPrime--)
			; // empty loop body
			System.out.println("Largest prime less than or equal to " + val + " = " + myPrime);
	}
}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.