[C/C++] code bài tập về mảng 1 chiều

HuanBuiThanh

đây là code của bài tập cô giao về nhà nhé .. từ câu 1 -> câu 6 :Phần  Mảng .

#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();
}

Getting Info...

تعليق واحد

  1. thanks bạn :)
Cảm ơn bạn đã quan tâm và gửi nhận xét tại
http://huanbuithanh.blogspot.com
hãy ghé thăm blog hàng ngày để được cập nhật những thủ thuật mới nhất nhé . thân ái !
Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.