publicclassDemo { publicstaticbooleanisPrime(int input) { intn= (int) Math.sqrt(input); if (input == 1 || input <= 0) System.out.println("error"); for (inti=2; i <= n; i++) { if (input % i == 0) returnfalse; } returntrue; }
publicstaticvoidmain(String[] args) { int input; Scanneruser=newScanner(System.in); System.out.print("please enter an integer:"); input = user.nextInt(); if (isPrime(input)) System.out.println(input + " is prime"); else System.out.println(input + " not prime"); } }
intisPrime(int input) { if (input == 1 || input <= 0) { printf("error"); exit(0); } int n = sqrt(input); for (int i = 2; i <= n; i++) { if (input % i == 0) { return1; //若条件满足,能被其他数整除,不是素数 } } return0; }
intmain() { int input, i; printf("please enter the data:"); scanf("%d", &input); if (isPrime(input)) printf("%d is prime", input); else printf("%d not prime", input); return0; }