Skip to content

Instantly share code, notes, and snippets.

@imryan
Last active December 26, 2015 11:09
Show Gist options
  • Save imryan/7141602 to your computer and use it in GitHub Desktop.
Save imryan/7141602 to your computer and use it in GitHub Desktop.
Prime number calculator.
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