WAP to accept a number and check if it is a prime number
Answers
Answered by
1
Answer:
import java.util.*;
class PrimeCheck
{
public static void main(String ar[])
{
Scanner sc=new Scanner (System.in);
System.out.println("Enter a no.");
int c=0,i,n=sc.nextInt();
for(i=1;i<=n;i++)
{
if(n%i==0)
c++;
}
if (c==2)
System.out.println("Prime");
}
}
- For verification please see the above attachment
Attachments:
Answered by
4
Answer:
import java.until.*;
class prime_number
{
public static void main (String args[])
{
Scanner sc=new Scanner (System.in);
int c=0;
{
System.out.println ("Enter a number");
int n=sc.nextInt();
for (i=1;i<=n;i++)
{
if (n%i==0)
c++;
}
if(c==2)
System.out. println ( n+ "Is a prime number");
else
System.out.println(n+ " Is not a prime number ");
}
}
Output
3
Input
3 is a prime number
Similar questions
Geography,
2 months ago
Math,
2 months ago
Math,
5 months ago
Hindi,
11 months ago
CBSE BOARD XII,
11 months ago