L1-028. Judge prime number, l1-028 judge Prime Number

Source: Internet
Author: User

L1-028. Judge prime number, l1-028 judge Prime Number

The objective of this question is to determine whether a given positive integer is a prime number.

Input Format:

The input returns a positive integer N (<= 10) in the first row, followed by N rows. Each row returns a positive integer smaller than 231.

Output Format:

For each positive integer to be judged, if it is a prime number, "Yes" is output in a row; otherwise, "No" is output ".

Input example:

211111

Output example:

YesNo

Time Limit: 400 ms memory limit: 65536 kB code length limit: 8000 B Judgment program Standard author Chen Yue Note: (1) the number of questions required to be input is a positive integer, so we need to consider the case of 1, 1 is not a prime number; (2) when determining whether a number is a prime number, you only need to traverse from 2 to the square root of the number. If there is a fully divided number, you can exit the loop. This reduces the time complexity.
#include <stdio.h>#include <stdlib.h>#include <math.h>int main(){    int n;    int i, j;    int arr[10];    int flog = 0;    scanf("%d", &n);    if(n > 10 || n < 0)        exit(0);    for(i = 0; i < n; i++)    {        scanf("%d", &arr[i]);    }    for(i = 0; i < n; i++)    {        if(arr[i] == 1)            printf("No\n");        else{        flog = 0;        for(j = 2; j <= sqrt(arr[i]); j++)        {            if(arr[i]%j == 0)            {                flog = 1;                break;            }        }        if(flog == 1)            printf("No\n");        else            printf("Yes\n");        }    }    return 0;}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.