To Check entered no is prime or not.
import java.util.Scanner;
public class Prime {
public static void main(String[] args) {
System.out.print("enter a no to check :Is it a prime no? ");
Scanner in=new Scanner(System.in);
int num=in.nextInt();
int flag=0;
for(int i=2;i<num;i++)
{
if(num%i==0)
{
System.out.print("not a prime no");
flag=1;
break;
}
}
if(flag==0);
System.out.print(" prime no");
}
}
No comments:
Post a Comment