Computer Science, asked by priyanshusingh2286, 11 months ago

write a computer java program to find whether number is prime or not​

Answers

Answered by bhagatg433
1

Answer:

Prime Number Program in Java

public class PrimeExample{

public static void main(String args[]){

int i,m=0,flag=0;

int n=3;//it is the number to be checked.

m=n/2;

if(n==0||n==1){

System.out.println(n+" is not prime number");

}else{

Answered by Anonymous
0

\huge\bold\red{HELLO!}

<marquee>ItzHarsh★</marquee>

import java.util.Scanner;

class PrimeCheck

{

public static void main(String args[])

{

int temp;

boolean isPrime=true;

Scanner scan= new Scanner(System.in);

System.out.println("Enter any number:");

//capture the input in an integer

int num=scan.nextInt();

scan.close();

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

{

temp=num%i;

if(temp==0)

{

isPrime=false;

break;

}

}

//If isPrime is true then the number is prime else not

if(isPrime)

System.out.println(num + " is a Prime Number");

else

System.out.println(num + " is not a Prime Number");

}

}

Output:

Enter any number:

19

19 is a Prime Number

Output 2:

Enter any number:

6

6 is not a Prime Number

You can also use while loop to check the prime number:

Just replace this part of the code in above program:

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

{

temp=num%i;

if(temp==0)

{

isPrime=false;

break;

}

}

with this:

int i=2;

while(i<= num/2)

{

if(num % i == 0)

{

isPrime = false;

break;

}

i++;

}

Similar questions