“Prime -Zahlen -Programm in Java” Code-Antworten

Primzahlprogramm in Java

// The following method returns a boolean indicating
// if the parameter is a prime number
private boolean isPrimeNaiveIter(int val) {
		if(val == 0 || val == 1)
			return false;
		if(val == 2)
			return true;
        // Look for a divisor other than 1 and val
		for(int div = 2; div*div <= val; div++) {
        	// if found return false
			if(val % div==0)
				return false;
		}
        // No divisors found => return it is a prime number
		return true;
}
Wissam

So finden Sie Primzahlen in Java

public class PrimeExample2{    

  static void checkPrime(int n){  
    int i,m=0,flag=0;      
    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  
  }  
 public static void main(String args[]){    
  checkPrime(1);  
  checkPrime(3);  
  checkPrime(17);  
  checkPrime(20);  
}    
}   
android developer

Prime -Zahlen -Programm 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{  
      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  
  }    
}  
Bredo

Ähnliche Antworten wie “Prime -Zahlen -Programm in Java”

Fragen ähnlich wie “Prime -Zahlen -Programm in Java”

Weitere verwandte Antworten zu “Prime -Zahlen -Programm in Java” auf Java

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen