L1-028 Judgment prime number (10 minutes)

The goal of this question is very simple, which is to determine whether a given positive integer is a prime number.

Input format:
input a positive integer N (≤ 10) in the first line, and then N lines, each line gives a positive integer less than 2
​31
​​ that needs to be judged.

Output format:
For each positive integer that needs to be judged, if it is a prime number, output Yes in one line, otherwise output No.

Input sample:
2
11
111
Output sample:
Yes
No

#include <stdio.h>
#include <math.h>
int isPrime(int p);
int main()
{
    
    
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; i++)
    {
    
    
        int p;
        scanf("%d", &p);
        if (isPrime(p))
            printf("Yes\n");
        else
            printf("No\n");
    }

	return 0;
}
int isPrime(int p)
{
    
    
    if (p <= 1)
        return 0;
    for (int div = 2; div <= sqrt(p); div++)
        if (p % div == 0)
            return 0;
    return 1;
}

Guess you like

Origin blog.csdn.net/fjdep/article/details/115307375