0% found this document useful (0 votes)
38 views

Tim Max Min Cua Mang

The document contains code for a C program that defines functions to: 1) Input values into an integer array 2) Find the smallest positive number in the array 3) Calculate the sum of negative numbers in the array 4) Count the prime numbers in the array The main function displays a menu and calls the appropriate function based on the user's selection.

Uploaded by

tuyendk7
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Tim Max Min Cua Mang

The document contains code for a C program that defines functions to: 1) Input values into an integer array 2) Find the smallest positive number in the array 3) Calculate the sum of negative numbers in the array 4) Count the prime numbers in the array The main function displays a menu and calls the appropriate function based on the user's selection.

Uploaded by

tuyendk7
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include "stdio.

h"
#include "conio.h"
#include "math.h"

int mang[50],n;

void NhapMang();
void TimSoDuongNhoNhat();
void TinhTongCacSoAm();
void DemSoNguyenTo();

void main()
{
    char c;
    while(1)
    {
        clrscr();
        printf("\t\tMENU\n\n");
        printf("1.Nhap mang so nguyen\n");
        printf("2.Tim so nguyen duong nho nhat\n");
        printf("3.Tinh tong cac so am\n");
        printf("4.Dem so nguyen to\n");
        printf("5.Thoat\n\n");
        printf("Moi ban chon chuc nang:");
        fflush(stdin);c=getch();
        switch(c)
        {
            case '1':
                NhapMang();
                break;
            case '2':
                TimSoDuongNhoNhat();
                break;
            case '3':
                TinhTongCacSoAm();
                break;
            case '4':
                DemSoNguyenTo();
                break;
            case '5':return;
        }
    }
}

void NhapMang()
{
    int i;
    clrscr();
    printf("\t\tVAO DU LIEU CHO MANG\n\n");
    do
    {
        printf("So phan tu:");
        scanf("%d",&n);
        if(n<0||n>50) printf("Nhap sai.Nhap lai\n");
    }
    while(n<0||n>50);
    printf("Nhap gia tri cac phan tu:\n");
    for(i=0;i<n;i++)
    {
        printf("Phan tu thu %d:",i+1);
        scanf("%d",&mang[i]);
    }
    printf("Da nhap du lieu xong.An phim bat ki de ra menu
chinh");
    fflush(stdin);getch();
}

void TimSoDuongNhoNhat()
{
    int so=0,i;
    clrscr();
    printf("\t\tTIM SO NGUYEN DUONG NHO NHAT\n\n");
    for(i=0;i<n;i++)
        if(mang[i]>0&&(mang[i]<so||so==0))
            so=mang[i];
    if(so>0) printf("So nguyen duong nho nhat la:%d\n",so);
    else printf("Khong co so nguyen duong nao trong mang\n");
    printf("An phim bat ki de quay ve menu chinh");
    fflush(stdin);getch();
}

void TinhTongCacSoAm()
{
    int tong=0,i;
    clrscr();
    printf("\t\tTINH TONG CAC SO NGUYEN AM\n\n");
    for(i=0;i<n;i++)
        if(mang[i]<0) tong+=mang[i];
    if(tong<0) printf("Tong cac so nguyen am la:%d\n",tong);
    else printf("Khong co so nguyen am nao trong mang\n");
    printf("An phim bat ki de quay ve menu chinh");
    fflush(stdin);getch();
}
int NguyenTo(int num);
void DemSoNguyenTo()
{
    int so=0,i;
    clrscr();
    printf("\t\tDEM SO CAC SO NGUYEN TO\n\n");
    for(i=0;i<n;i++)
        if(NguyenTo(mang[i])) so++;
    printf("Co %d so nguyen to trong mang\n",so);
    printf("An phim bat ki de quay ve menu chinh");
    fflush(stdin);getch();
}

int NguyenTo(int num)


{
    int i;
    if(num<2) return 0;
    for(i=2;i<=sqrt(num);i++)
        if(num%i==0) return 0;
    return 1;
}

You might also like