Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

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;
	}
}
Source by www.w3schools.in #
 
PREVIOUS NEXT
Tagged: #java #check #prime #number
ADD COMMENT
Topic
Name
9+6 =