Prime Number in Java


Write a Program in Java of Prime Number.

Prime Number :-

A number is called prime number if its not divisible by any number other than 1 or itself and you can use this logic to check whether a number is prime or not.

 

import java.util.*;

class Prime

{

public static void main(String args[])

{

int n, i, res;

boolean flag=true;

Scanner scan= new Scanner(System.in);

System.out.println(“Please Enter a No.”);

n=scan.nextInt();

for(i=2;i<=n/2;i++){

res=n%i;

if(res==0)

{

flag=false;

break;

}}

if(flag)

System.out.println(n + ” is Prime Number”);

else

System.out.println(n + ” is not Prime Number”);

}}

output: –

pp

Related Posts

Comments are closed.