# C语言基础习题50例（八）36-40

## 习题36

#include <stdio.h>
#include <math.h>

int main(){
int isPrime(int n);
int i, count = 0;
for(i = 2; i < 101; i++){
if(isPrime(i)){
count++;
printf("%5d", i);
if(count % 5 == 0){
printf("\n");
}
}
}

return 0;
}

int isPrime(int n){
int i, prime = 1;
for(i = 2; i <= sqrt(n); i++){
if(n % i == 0){
prime = 0;
break;
}
}
return prime;
}


    2    3    5    7   11
13   17   19   23   29
31   37   41   43   47
53   59   61   67   71
73   79   83   89   97



## 习题37

#include <stdio.h>

int main(){
void sort(int ua[], int l);
int i, unsorted_list[] = {12, 54, 81, 3, 72, 47, 99, 32, 41, 62}, *p;
printf("Unsorted:\n");
for(i = 0; i < 10; i++){
printf("%d ", unsorted_list[i]);
}
p = unsorted_list;
int length = sizeof(unsorted_list) / sizeof(unsorted_list[0]);
sort(p, length);
printf("\nAfter sorted:\n");
for(i = 0; i < 10; i++){
printf("%d ", unsorted_list[i]);
}

return 0;
}

void sort(int ua[], int l){
int i, j, temp;
for(i = l - 2; i >= 0; i--){
for(j = 0; j <= i; j++){
if(ua[j] > ua[j + 1]){
temp = ua[j];
ua[j] = ua[j + 1];
ua[j + 1] = temp;
}
}
}
}


Unsorted:
12 54 81 3 72 47 99 32 41 62
After sorted:
3 12 32 41 47 54 62 72 81 99


## 习题38

#include <stdio.h>

int main(){
int a[3][3] = {0}, i, j, sum = 0;
for(i = 0; i < 3; i++){
for(j = 0; j < 3; j++){
scanf("%d", &a[i][j]);
}
}
for(i = 0; i < 3; i++){
for(j = 0; j < 3; j++){
if(i == j){
sum += a[i][j];
}
}
}
printf("Sum = %d\n", sum);

return 0;
}


Please input the 9 numbers:
1 2 3 4 5 6 7 8 9
Sum = 15



## 习题39

#include <stdio.h>

int main(){
int a[11] = {1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 0}, num, i, j;
printf("Please input the number to insert:\n");
scanf("%d", &num);
if(num >= a[9]){
a[10] = num;
}else{
i = 9;
while(a[i] > num){
i--;
}
for(j = 10; j > i + 1; j--){
a[j] = a[j - 1];
}
a[i + 1] = num;
}
for(i = 0; i < 11; i++){
printf("%d ", a[i]);
}

return 0;
}


Please input the number to insert:
50
1 4 9 16 25 36 49 50 64 81 100


## 习题40

#include <stdio.h>
#define N 10

int main(){
int a[N] = {1, 4, 9, 16, 25, 36, 49, 64, 81, 100}, num, i, j, temp;
printf("Normal order:\n");
for(i = 0; i < 10; i++){
printf("%d ", a[i]);
}
for(i = 0; i < N / 2; i++){
temp = a[i];
a[i] = a[ N - 1 - i];
a[ N - 1 - i] = temp;
}
printf("\nReversed order:\n");
for(i = 0; i < 10; i++){
printf("%d ", a[i]);
}

return 0;
}


Normal order:
1 4 9 16 25 36 49 64 81 100
Reversed order:
100 81 64 49 36 25 16 9 4 1


09-04 18万+
11-14 240
01-29
03-28
01-06
09-15 55
11-20 2864
05-31
05-04 181
07-10 1715