write Java program for to enter a number and check that number is prime or not
Answers
Answered by
2
public class Prime { public static void main(String[] args) { int num = 33, i = 2; boolean flag = false; while(i <= num/2) { // condition for nonprime number if(num % i == 0) { flag = true; break; } ++i; } if (!flag) System.out.println(num + " is a prime number."); else System.out.println(num + " is not a prime number."); } }
Answered by
6
Heya,
______________________________
______________________________
Here is your answer↓↓↓↓↓
class prime
{
public static void main(int n)
{
int c=0;
for(int i = 1; i<=n; i++)
{
if(n%i==0)
c= c+1;
}
if(c==2)
System.out.println("it is prime");
else
System.out.println("it is composite");}}
Then enter the value and you will get to know .
_________________________________
_________________________________
ANKIT
______________________________
______________________________
Here is your answer↓↓↓↓↓
class prime
{
public static void main(int n)
{
int c=0;
for(int i = 1; i<=n; i++)
{
if(n%i==0)
c= c+1;
}
if(c==2)
System.out.println("it is prime");
else
System.out.println("it is composite");}}
Then enter the value and you will get to know .
_________________________________
_________________________________
ANKIT
Attachments:
Similar questions