Tìm kiếm

Âm lịch

Translate

Java: Kiểm tra số nguyên tố

package kiemtrasonguyento;
import java.util.Scanner;
public class Kiemtrasonguyento 
{
    public static void main(String[] args) 
    {
        int n; int kiemtra=1;
        System.out.print("Nhap so nguyen: ");
        Scanner sc= new Scanner(System.in);
        n=sc.nextInt();

        for(int i=2;i<n;i=i+1)
            {if (n%i==0){kiemtra=0; break;}}
        
        if (kiemtra==1) System.out.println(n+" la so nguyen to");
        if (kiemtra==0) System.out.println(n+" Khong phai nguyen to");      
     }
}