Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

prime check java

  static boolean isPrime(int n){
        boolean result=true;
        if(n <= 1) return false;
        for(int i = 2; i <= n/2; i++){ //n/2 because the largest factor of any number is either it's half or less than it
            if(n % i == 0){
                result = false; //if some number divides it, then prime=false;
                break;
            }
        }
        return result;
    }
Comment

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

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

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

Program to check whether the number is prime or not in java

#include <stdio.h>void main(){int value, rem;printf("Enter an int ");scanf("%d", &value);rem = value % 2;if (rem == 0)printf("%d is an even int
", value);elseprintf("%d is an odd int
", value);}
Comment

PREVIOUS NEXT
Code Example
Java :: android studio checkbox result 
Java :: priority queue java 
Java :: Set icon for toolbar android 
Java :: custom dialog in android 
Java :: double to int 
Java :: java insertion sort 
Java :: look and feel java 
Java :: Java Scanner nextInt() 
Java :: java stream findany 
Java :: get selected item spinner 
Java :: int arr = new int 
Java :: meaning of instantiated in java 
Java :: how to set the length to int array in java 
Java :: import string java 
Java :: java append to array 
Java :: how to remove leading space in java 
Java :: singleton 
Java :: advantages of using java 
Java :: java interview questions for freshers 
Java :: javafx listview of objects 
Java :: save file to disk java 
Java :: output of this 
Java :: receive an int from terminal java 
Java :: spring boot initializr-generator example 
Java :: Java extends and implements clause 
Java :: Deque interface in Java 
Java :: What is the use of @Listener annotation in TestNG? 
Java :: java GLIBC 2 34 missing 
Java :: class c { public static void main(string[] args){ system.out.println("hello"+args[0]);}} output 
Java :: how to make easy animations in canva 
ADD CONTENT
Topic
Content
Source link
Name
5+5 =