![]() |
![]() |
A prime number is a positive integer which is divisible by itself and only 1
See the following flowchart to clearly understand the working of loop to find out number is prime or not.
import java.util.Scanner; public class PrimeNumberEx { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n,i,flag; i = 2; flag = 0; System.out.print("Enter any number : "); n = scan.nextInt(); while(i<=n/2) { if(n%i == 0) { flag = 1; break; } i = i + 1; // or i += 1; } if(flag==0) System.out.print(n+" is a prime number"); else System.out.print(n+" is Not prime number"); } }