C language prime, how to tell if a number is prime in C language?

Updated on technology 2024-05-11
6 answers
  1. Anonymous users2024-02-10

    You can add a 0 to the end of the array, but you can't have a 0 in the middle. Then change it to something like this:

    #include

    int main()

    int n, i,k, a=;There is an extra 0n=0; Assign an initial value of 0 to n

    while(a[n]) When a[n] is true, i.e. a[n] is not 0

    k=sqrt(a[n]);Here we use a[n]for(i=2; i<=k;i++)

    if(n%i==0)break;

    if(i>k)

    printf("%d ",n);

    n++;n is incremented by 1 so that a[n] is the next number.

  2. Anonymous users2024-02-09

    The program is not very well written.

    First of all, how can you use your printf without include.

    Secondly, the return value of the main() function is int, but you don't have a return.

    Again, what does n=a mean, I haven't seen k is int, but the return value of sqrt() is double.

    Finally, the for loop seems to only be able to judge one number, if it could.

  3. Anonymous users2024-02-08

    lz has learned arrays, and he can still write such a bad program, which is admired!

    After all, men become big things. Laying a good foundation is key!

    Don't forget!

  4. Anonymous users2024-02-07

    #include ""

    void main()

    If an integer is prime, then only it is divisible by 1 and itself, so there is only 2, and if it exceeds 2, then it is certainly not prime.

    if (flag==2) else}

  5. Anonymous users2024-02-06

    #include

    #include

    #include

    int prime(int a) defines a function to determine whether a is prime or not

    if (flag==2)

    return 1;*If a is a prime number, the flag should be 2 and the return value is 1*

    elsereturn 0;*If a is not prime, the flag is greater than 2 and the return value is 0*

    * (This part is supplemental, the prime function can be simplified to :)int prime(int a).)

    return flag;}*/

    int main()

  6. Anonymous users2024-02-05

    For details, please refer to:

    Prime numbers areOnlyA positive integer divisible by 1 and itself. For example, 2 is a prime number: 2 1=2, 2 2=1, i.e., 2 is divisible by 1 and is 2, and 2 is divisible by 1 and itself, so it is a qualitative curved number.

Related questions
12 answers2024-05-11

This is an additive function I wrote that can judge overflow: >>>More

15 answers2024-05-11

Indicates that the remainder of 6 is taken.

In the C language, % stands for remainder (or modulo operation). >>>More

11 answers2024-05-11

Valid variable names for the C language:

First, it can only contain numbers, letters, and underscores. >>>More

6 answers2024-05-11

It seems that you don't know much about structs and struct pointers, the data array is a struct array that you define, it consists of two knot body elements, and each struct element contains two members x and y, the first element is 1 and 10, the second element is 2 and 20, the struct pointer p you defined starts to point to the first element of the array data, p points to the second element of the data array, and the pointer p can manipulate the two members x and y >>>More

8 answers2024-05-11

The semicolon is the sign of the end of the statement, but the semicolon is not used after the loop, if, and subfunctions such as long long a (int b), and everything else is used, but two semicolons cannot be added, although it will not cause an error, but it may affect the result.