Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to exchange prime numbers in arrays in C language

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report