Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update PrimeNo.java in java with Explanation via comments in code #21

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 28 additions & 14 deletions PrimeNo.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,33 @@
import java.util.Scanner;
public class PrimeNo{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int count=0;
for(int i=1; i<=n; i++){
if(n%i==0){
count++;

public class PrimeNo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(); // Read an integer from the user's input.
int count = 0; // Initialize a count variable to 0 to count the number of divisors.

for (int i = 1; i <= n; i++) {
if (n % i == 0) {
count++; // If 'n' is divisible by 'i' without a remainder, increment the count.
}
}
if(count==2){
System.out.println("Prime");
}
else{
System.out.println("Not Prime");

if (count == 2) {
System.out.println("Prime"); // If 'count' is equal to 2, it's a prime number.
} else {
System.out.println("Not Prime"); // If 'count' is not 2, it's not a prime number.
}
}
}
}

/*
Explanation:

The program starts by importing the Scanner class to read input from the user.
It uses the Scanner class to read an integer (n) from the user.
A count variable is initialized to 0. This variable will be used to count the number of divisors of the input number.
The program enters a for loop that iterates from i = 1 to i <= n. It checks each number from 1 to n to see if it's a divisor of n.
Inside the loop, the if statement checks if n is divisible by i (i.e., if n % i == 0). If it is, the count variable is incremented by 1.
After the loop, the program checks if the count variable is equal to 2. If it is, it means that the input number has only two divisors (1 and itself), so it prints "Prime."
If the count variable is not equal to 2, it means the input number has more than two divisors, so it prints "Not Prime."
*/