Computer Science, asked by kakakakakkasfdg, 1 year ago

Write a program in java to check prime numbers

Answers

Answered by KshitijMaurya
1
hola

TO check it following is program to check whether a number is a prime or not

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{ 
 
   for(i=2;i<=m;i++){
      
    if(n%i==0){       
    System.out.println(n+" is not prime number"); 
     
     flag=1;         
  break;      
    }         }        
 if(flag==0)  { System.out.println(n+" is prime number"); }
  
  }//end of else  
}   
 }   

Anonymous: OMG THATS TOO DIFFICULT
KshitijMaurya: its very much easy
Answered by Anonymous
1
// This program is to check whether the given number is prime number or not
class Prime_number
{
public void main(int n)
{
int i, c=0;
for (i=1;i<=n;i++)
{
if (i%n == 0)
c++;
}
if (c==2)
{
System.out.println("The given number is a prime number");
}
else
{
System.out.println("The given number is not a prime number");
}
}
}

Here
Variable n : to take a number from user
              i  : for increment
              c : to count how many times it is equal to zero


Anonymous: thanks
Similar questions