#include<stdio.h>
#include<conio.h>
#include <math.h>
/*nhap vao mang*/
void nhap(int a[],int n)
{
int i;
for(i=0;i<n;i++)
{
printf("vui long nhap vao gia tri cua phan tu thu[%d]:",i+1);
scanf("%d",&a[i]);
}
}
/*xuat mang*/
void xuat(int a[],int n)
{
int i;
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
}
//kiem tra so nguyen to
int ktSNT (int a)
{
int i = 0;
for(i = 2; i <= (int)sqrt(a); i++)
{
if( a % i == 0 || a < 2 && a != 2)
{
return 0;
}
}
return 1;
}
// tim UCLN , BCNN
//-----------------------
int TimUCLN(int a[])
{
int UC;
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
{
while(a[i]!=a[j]) a[i]>a[j]?(a[i]=a[i]-a[j]):(a[j]=a[j]-a[i]);
UC=a[i];
}
printf("%d",a[i]);
return (UC);
}
//------------------------
int TimBCNN(int a[])
{
int BC;
j=1;
for(i=0;i<n;i++)
{
while(j%a[i]!=0) j++;
BC=j;
print("%d",BC);
}
return (BC);
}
//-------------------------
// CHUONG TRINH CHINH
void main()
{
clrscr();
int n,vt,i,j,so,k;
int tg;//danh cho sap xep tang dan
int tong; // danh cho tinh tong cac phan tu cua mang
// int snt[100];
int a[100];
printf("vui long nhap vao n:");
scanf("%d",&n);
nhap(a,n);
printf("Mang sau khi nhap la:\n");
xuat(a,n);
//kiem tra so nguyen to
printf("\n cac so nguyen to la : ");
for(i = 0; i < n; i++)
{
if(ktSNT(a[i]) == 1)
{
printf("%d",a[i]);
printf(" ");
}
}
// sap xep tang dan
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if(a[i]>a[j])
{
tg=a[i];
a[i]=a[j];
a[j]=tg;
}
printf("\nMang sap xep tang dan la:");
for(i=0;i<n;i++)
printf("\t%d",a[i]);
// tinh tong cac phan tu cua mang
tong=0;
for(i=1;i<n;i++)
tong= tong + a[i];
printf("\n tong cac phan tu cua mang la : %d",tong);
//tim UCLN , BCNN
printf("\nUCLN=%d",TimUCLN(a));
printf("\nBCNN=%d",TimBCNN(a));
// ket thuc chuong trinh
getch();
}
[C/C++] code bài tập về mảng 1 chiều
HuanBuiThanh
Estimated read time: 1 min
Getting Info...