Get the App
SLTechnology News&Howtos  ›  Development  › 

How to exchange prime numbers in arrays in C language

Shulou Source: shulou.com Published: 2022-06-02 06:56:58 09月30日 Update

How to exchange primes in arrays in C language, I believe many inexperienced people are helpless about this, this article summarizes the causes and solutions of the problem, through this article I hope you can solve this problem.

requirements of the topic

(1) Create array num in the main function, initialize it with test data {108 4 19 7 23 66 49 13 33 35};

(2) Define the change function, which exchanges the primes in num array from beginning to end, that is, the first prime number is exchanged with the last prime number, the second prime number is exchanged with the penultimate prime number, and the non-prime number remains unchanged.

(3) Define prime function, which is used to judge prime numbers.

#include #include int prime(int num){ int i,k; if(num>=2) { k=sqrt(num); for(i=2;i=k+1) return 1; else return 0; } else return 0;} void change(int* a,int* b){ //defines pointer a,b of type int*, pointer points to type int int tmp = *a; //assign the contents of the address pointed to by pointer a to tmp *a = *b; //assign the contents of the address pointed to by pointer b to the contents of the address pointed to by pointer b *b = tmp; }int main(){ int a[10]={108,4,19,7,23,66,49,13,33,35}; int i,j=0,t,p[10]; printf("array before swap: "); for(i=0;i

Tags: Prime array content pointer point function address language method more type problem helpless for this location reason this skills data articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Microsoft Huawei Docker Linux