Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java prime numbers


for(int i = 2 ; i <= num ; i++)
        {
            if(isPrime(i))
            {
                System.out.print("  " + i);
                cnt++;
            }
        }
        System.out.println("Prime numbers = " + cnt);
    }

    public static boolean isPrime(int i) {
        for (int j = 2; j <= Math.sqrt(i); j++) {

            if(i % j == 0)
            {
                return false;
            }

        }
        return true;
    }
Comment

printing prime numbers in java

import java.util.Scanner;
public class Main
{
   public static void main (String[] args)
   {		
      Scanner scanner = new Scanner(System.in);
      int i =0;
      int num =0;
      //Empty String
      String  primeNumbers = "";
      System.out.println("Enter the value of n:");
      int n = scanner.nextInt();
      scanner.close();
      for (i = 1; i <= n; i++)  	   
      { 		 		  
         int counter=0; 		  
         for(num =i; num>=1; num--)
         {
	    if(i%num==0)
	    {
		counter = counter + 1;
	    }
	 }
	 if (counter ==2)
	 {
	    //Appended the Prime number to the String
	    primeNumbers = primeNumbers + i + " ";
	 }	
      }	
      System.out.println("Prime numbers from 1 to n are :");
      System.out.println(primeNumbers);
   }
}
Comment

how to find the prime numbers in java

import java.util.*;
class Example{
	public static boolean isPrime(int num){
		for(int i=2; i<num; i++){
			if(num%i==0){
				return false; 
			}
		}
		return true;
	} 
	public static void main(String args[]){
		for (int i = 0; i < 1000; i++){
			if(isPrime(i)){
				System.out.println(i);	
			}
		}
	}
}
Comment

how to find prime numbers 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);  
}    
}   
Comment

prime number program 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;
}
Comment

how to print only prime numbers in java

public class PrimeNumbers {

 public static void main(String[] args) {

  int num = 20, count;

  for (int i = 1; i <= num; i++) {
   count = 0;
   for (int j = 2; j <= i / 2; j++) {
    if (i % j == 0) {
     count++;
     break;
    }
   }

   if (count == 0) {
    System.out.println(i+"is prime number");
   }

  }
 }
}
Program Output:
Comment

prime numbers 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{  
      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  
  }    
}  
Comment

java check prime number

import java.util.Scanner;
class main {
	public static void main(String [] args) {
		Scanner kb = new Scanner(System.in);
		System.out.print("? ");
		int num1 = kb.nextInt();
        boolean res = isprime(num1);
        System.out.println(res);
	}

	public static boolean isprime(int num1) {
        boolean ans = true;
        int i = 1;
        for(i = 2;i < num1;i++) {
            if((num1 % i == 0) && (num1 != 2)) {
                ans = false;
                break;
            }else{
                ans = true;
                break;
            }
        }
        return ans;
	}
}
Comment

PREVIOUS NEXT
Code Example
Java :: random int in range java 
Java :: arraylist .set java 
Java :: android sha1 key 
Java :: android java Check Wifi Connection 
Java :: read timed out android studio 
Java :: final in java 
Java :: how to reverse a string in java without function 
Java :: java convert double to int 
Java :: multiplication program java 
Java :: String remove duplicate method in java 
Java :: inheritance in oop 
Java :: retrieving parent element from child aWebelement selenium java 
Java :: method reference in java 
Java :: how to use user uid in android 
Java :: android menu change text color 
Java :: write in text file android studio 
Java :: how to remove all characters before a certain character from a string in java 
Java :: java stringbuilder 
Java :: check first character of string is number java 
Java :: declaration and definition of array in java 
Java :: how to add the last area of an array in java 
Java :: There is no client authentication. Try adding an appropriate authentication filter 
Java :: remove duplicate from string 
Java :: basic java programs 
Java :: java split string without removing 
Java :: breadth first search bst java 
Java :: java math ceil 
Java :: java list 
Java :: java instantiate a scanner 
Java :: jasypt-spring-boot 
ADD CONTENT
Topic
Content
Source link
Name
6+2 =