---
# System prepended metadata

title: 108年程式設計課堂練習參考解答

---

# 108年程式設計課堂練習參考解答
第一題
```
#include <stdio.h>

int is_prime(int num);
int inverse(int num);
int numOfFactor(int num);

int main(void){

    FILE* InverPrime_file_Pointer;


    int i;
    int sum = 0;

    InverPrime_file_Pointer = fopen("InverPrime.txt","w");
    if( InverPrime_file_Pointer == NULL){
        fprintf(InverPrime_file_Pointer,"Open failure.");
        return 1;
    }

    for(i = 10; i<100; i++){

        if(is_prime(i) && is_prime(inverse(i))){
            fprintf(InverPrime_file_Pointer,"%d ",i);
            sum += i;
        }
    }
    fprintf(InverPrime_file_Pointer,"\n%d\n",sum);

    return 0;
}

int is_prime(int num){

    if( !numOfFactor(num) ){
        return 1;
    }
    else return 0;
}

int inverse(int num){

    return (num%10)*10 + num/10;

}

int numOfFactor(int num){
    int i;
    int counter = 0;
    for( i = 2; i < num; i++){
        if( ! (num%i) )
            counter++;
    }
    return counter;
}

```
第二題
```
#include <stdio.h>
void stat(int n, int array[], float *ptr_Average, float *ptr_Variance);

int main(void){

    int data[7] = {21, 16, 41, 29, 19, 24, 43};
    float my_ptr_average;
    float my_ptr_variance;

    stat(7 , data, &my_ptr_average, &my_ptr_variance);
    printf("aveg: %f, vari: %f\n", my_ptr_average, my_ptr_variance);
    return 0;

}


void stat(int n, int array[], float *ptr_Average, float *ptr_Variance){

    int i ;
    float aveg=0;
    float vari=0;
    for( i = 0; i < n; i++){
        aveg += array[i];

    }
    aveg /= n;
    for( i = 0; i < n; i++){

        vari += (array[i] - aveg) * (array[i] - aveg);

    }
    *ptr_Average = aveg;
    *ptr_Variance = vari/aveg;

}

```
第三題
```
#include<stdio.h>
int g(int n);

int main(void){

    int i;
    FILE* fp;

    fp = fopen("Out.txt","w");
    if(fp == NULL){
        printf("open fail.");
        return 1;
    }

    for(i = 10000; i <= 99999; i++){
            fprintf(fp, "%d\n",g(i));
    }

    return 0;
}


int g(int n){
    int sum = 0;
    if ( n < 10 )
        return n;
    while( n > 0){
        sum += n%10;
        n /= 10;
    }
    return g(sum);
}

```
