Computer Science, asked by gunu6, 1 year ago

WAP to enter a number and check whether it is a prime no or not.

Answers

Answered by ArtificialWorld
2

#include <stdio.h>
int main()
{
int n, i, flag = 0;

printf("Enter a positive integer: ");
scanf("%d",&n);

for(i=2; i<=n/2; ++i)
{
// condition for nonprime number
if(n%i==0)
{
flag=1;
break;
}
}

if (flag==0)
printf("%d is a prime number.",n);
else
printf("%d is not a prime number.",n);

return 0;
}
Answered by crpiyushbansal
0

Answer:

public class PRIME_NOT_PRIME {

   public static void main(String[] args) {

       Scanner sc=new Scanner(System.in);

       System.out.println("ENTER THE NUMBER");

       int num=sc.nextInt();

       boolean condition=true;

       for(int i=2; i<num;i++){

           if(num%i==0){

               condition=false;

               break;

           }

       }

       if(condition && num>=3){

           System.out.println(num+" IS A PRIME NO.");

       }

       else{

           System.out.println(num+" IS NOT A PRIME NO.");

       }

   }

}

Explanation:

Similar questions