Hi, I've been given an assignment to write a java program that determines if a random given number is Prime OR not.
But we also have to make comments in each lines, explaining the function/purpose of each lines to this java coding.
So far, I've gotten the program corrected! All that's left is the comments. So, can anyone be kind enough as to go through my coding down here, and add/correct more suitable comments for each lines in this coding?
Here's the coding (with all the comments I made so far...):
import java.util.Scanner; //This line helps the compiler find the class file Scanner.
public class PrimeNumber{ //The class name here is PrimeNumber
public static void main(String args[]){ //This is the main method which is the entry point into this prime number coding program.
int n; //This is the declaration of the variable n.
Scanner Prime = new Scanner(System.in); //This allows users to read values of various types.
System.out.println(" Please enter a number: "); //This line prints out whatever is written between the 2 double codes in the brackets.
n = Prime.nextInt(); //A method that returns the next integer from the source.
if (isPrime(n))
System.out.println("The number is prime.\n"); //This line prints out whatever is written between the 2 double codes in the brackets.
else
System.out.println("The number is not prime.\n"); //This line prints out whatever is written between the 2 double codes in the brackets.
}
static boolean isPrime(int n) { //This is the starting of Boolean valued expressions used in this java coding.
if (n%2==0)return false; //This line checks odds up to the square root.
for(int i=3;i*i<=n;i+=2) { //This line defines an integer 'i' as the integer other than 1 and the given number. That means, i>2 and i<num.
if(n%i==0)
return false;
}
return true;
}
}
Need some help here, thanks in advance!