2. WAP in Java to check the no. given is prime number or not. using if else statement
Answers
Answer:
Simple methods. The simplest primality test is trial division: given an input number, n, check whether it is evenly divisible by any prime number between 2 and √n (i.e. that the division leaves no remainder). If so, then n is composite. Otherwise, it is prime.
Explanation:
might help you
The C0DE :
import java.util.Scanner;
public class CheckForPrimeNum {
public static void main(String[] args) {
int a, flag = 0;
Scanner input = new Scanner(System.in);
System.out.println("Enter the number to check for prime number");
a = input.nextInt();
for (int i = 2; i <= a; i++) {
if (a % i == 0) {
flag += 1;
}
}
if (flag == 1) {
System.out.println("The entered number is prime number");
} else {
System.out.println("The entered number is non prime");
}
input.close();
}
}
Example Output :
Enter the number to check for prime number
2
The entered number is prime number