Last active
December 26, 2015 11:09
-
-
Save imryan/7141602 to your computer and use it in GitHub Desktop.
Prime number calculator.
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; | |
public class Prime { | |
public static void main(String[] args) { | |
// Declare a new Scanner object and an integer | |
Scanner sc = new Scanner(System.in); | |
int n = 0; | |
// Read in a value for n | |
n = sc.nextInt(); | |
// Output the result of the isPrime() method | |
System.out.println(isPrime(n)); | |
} | |
public static boolean isPrime(int n) | |
{ | |
// Loop until i is n, starting at 2 | |
for (int i = 2; i <= n; i++) | |
{ | |
// Return true/false based on if the remainder of n and i is 0 | |
return (n % i == 0); | |
} | |
// Otherwise, return true | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment