c outputs prime numbers in an array

Updated on technology 2024-04-30
5 answers
  1. Anonymous users2024-02-08

    Ok, here's the **:

    #include

    int main()

    int len = sizeof(arr) / sizeof(arr[0]);

    int flag;

    printf("the prime numbers in the array are: ");

    for (int i = 0; i < len; i++)

    flag = 0;

    for (int j = 2; j <= arr[i] / 2; j++)

    if (arr[i] %j == 0) {

    flag = 1;

    break;

    if (flag == 0 &&arr[i] != 1)

    printf("%d ", arr[i]);

    return 0;

    The program first defines an integer array arr, then calculates the array length len, then iterates through each element through a nested loop to determine whether the element is prime, and finally outputs a prime number. The inner loop is used to determine whether an element is prime, and if it is divisible by any integer between 2 and (i 2), then the element is not prime. Finally, if the flag value is 0 and the element is not 1, it indicates that the element is a prime.

  2. Anonymous users2024-02-07

    1 According to the mathematical definition, write to judge prime numbers.

    functions. 2 pairs of integers are traversed starting from 2 and judged to be prime one by one.

    3 If it is prime, it is saved in the array, otherwise it continues.

    4 When the number of primes reaches 20, exit the traversal program.

    5 Output 20 prime numbers.

    **:#include <

    #include

    int is_prime(int n)

    int i;

    for(i = 2; i <= sqrt(n); i ++if(n%i == 0) return 0;

    return 1;

    int main()

    int i,a[20],n;

    for(i = 0,n=2; i<20; n++)if(is_prime(n))a[i++]= n;

    for(i = 0; i<20; i++)printf("%d,",a[i]);

    return 0;

  3. Anonymous users2024-02-06

    #include

    #define size 20

    int panduan(int num) is a prime number that returns 1, otherwise returns 0

    int i;

    if(num==1||num==2) 1 and 2 are prime numbers return 1;

    for(i=2;iif(num%i==0)

    return 0;

    return 1;

    void main()

    int num[size],i,j=0;

    for(i=1;j<20;i++)

    if(panduan(i))

    num[j]=i;

    j++;for(i=0;i<20;i++)printf("%d ",num[i]);

  4. Anonymous users2024-02-05

    Write a function implementation to determine whether an integer is prime or not. If the output is also in the function, you need to pass the array to the function, otherwise you just need to pass the array elements.

    The problem with your original program is:

    1.Include is missing""。Because sqrt();

    2.if(ss(a[i])==1) in the if(ss(a[i])==1) statement, the format of the call function ss does not match the definition: a[i] cannot be passed to a, and the second argument is missing.

    3.When judging prime numbers, the handling of the number 1 is not taken into account.

    If printing is also done in a function, the original program can be changed to:

    #include""

    #include""Increase.

    main()}

  5. Anonymous users2024-02-04

    The landlord's question is a little strange, to find all the prime numbers in 1 n can not use arrays, do you want to plug arrays in?

    #include

    #include

    int main(void)

    if(flag)

    printf("%d\t",i);

    i++;printf("");

    return 0;}

Related questions
10 answers2024-04-30

Here's a C implementation that assigns an array a[10] to p[n]: >>>More

14 answers2024-04-30

function assoc_unique($arr, $key) {

tmp_arr = array(); >>>More

12 answers2024-04-30

One-dimensional arrays hold the same values, but in the case of matrices, determinants. >>>More

3 answers2024-04-30

Summary. Here's a simple clock written in C** that prints out the current time (in hours, minutes, and seconds): include include int main() return 0; This ** uses the time function and the localtime function in the header file, as well as the printf function and the fflush function in the header file. >>>More