Created
August 12, 2015 04:51
-
-
Save xXFracXx/32dee9f7df7620228ce0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
import java.util.*; | |
import static java.lang.System.out; | |
public class Ques4 { | |
public static void main(String[] Args) { | |
Scanner in = new Scanner(System.in); | |
int n, c; | |
boolean ch; | |
out.print("Enter Number: "); | |
n = in.nextInt(); | |
ch = isPrime(n); | |
if(ch) | |
out.println("is Prime!"); | |
else | |
out.println("isn't Prime!"); | |
for(int i = 3; i <= n; i++) | |
if(isPrime(i)) | |
out.print(i + " "); | |
} | |
private static boolean isPrime(int num) { | |
if (num == 2 ) return true; | |
if (num % 2 == 0) return false; | |
for (int i = 3; i * i <= num; i += 2) | |
if (num % i == 0) return false; | |
return true; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment