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;
}
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;
}
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);
}
}
}
}
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);
}
}
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;
}
}
#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);}