TÍNH TỔNG CÁC SỐ NGUYÊN TỐ TRONG MẢNG 1 CHIỀU


CHƯƠNG TRÌNH MẪU

Code:

#include "conio.h"
#include "stdio.h"
#include "math.h"
#define max 100

//nhap mang
void NhapMang(unsigned int A[], unsigned int n) {

    for(int i = 0; i<n; i++)
    {
        printf("phan tu %d = ",i);
        scanf("%d",&A[i]);
    }
}
//xuat mang
void XuatMang(unsigned int A[], unsigned int n) {
    for(int i = 0; i<n; i++)
        printf("%d\t",A[i]);

}
//kiem tra nguyen to
char PrimeQ(unsigned int n) {
    char temp = 1;
    int i = 2;
    float t = sqrt(n);
    while(i<=t && temp)
    {
        if(n%i==0)
            temp = 0;
        else
            i++;
    }
    return temp;
}
//ham tinh tong tat ca cac so nguyen to trong mang
unsigned int S(unsigned int A[], int n){
    unsigned int temp = 0;
    for(int i = 0; i<n; i++)
        if(PrimeQ(A[i]))
            temp+= A[i];
    return temp;
}
//chuong trinh chinh
void main(){
    clrscr();
    unsigned int B[max], n;
    printf("Nhap n = ");
    scanf("%d",&n);
    NhapMang(B,n);
    printf("Mang vua nhap:\n");
    XuatMang(B,n);
    printf("\nTong cac so nguyen to = %d",S(B,n));
    getch();
}