0% found this document useful (0 votes)
25 views4 pages

BTVN CSLT C6

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views4 pages

BTVN CSLT C6

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

Bài 1:

#include<stdio.h>
int shc(int n)
{

int S=0;
for(int i=1; i<n; i++)
{
if(n%i==0) S+=i;
}
if(n==S) return 1;
else return 0;
}
int main()
{
for(int i=1; i<=1000; i++)
{
if(shc(i)==1) printf("%d ", i);
}

}
Bài 2:
#include<stdio.h>
#include<math.h>
int snt(int n)
{
for(int i=2; i<=sqrt(n); i++)
{
if(n%i==0) return 0;
}
return 1;
}
int main()
{
int count=0;
for(int i=1; i<=1000;i++)
{
if(snt(i)==1)
{
printf("%d ", i);
count++;
if(count%10==0)
printf("\n");
}
}
}
Bài 3:
#include<stdio.h>
#include<math.h>
int scp(int n)
{
int a= (int)sqrt(n);
for(int i=1; i<=n; i++)
{
if( a * a == n) return 1;
else return 0;
}

}
int main()
{
int count=0;
for(int i=1; i<=1000;i++)
{
if(scp(i)==1)
{
printf("%d ", i);
count++;
if(count%15==0)
printf("\n");
}
}
}
Bài 4:
#include<stdio.h>
int n;
void sophantu()
{
printf("so phan tu trong mang la: ");
scanf("%d", &n);
}
void nhapvao(int a[], int n)
{
printf("nhap vao cac phan tu trong mang:\n");
for(int i=0; i<n; i++)
{
scanf("%d", &a[i]);
}
}
void hienthi(int a[], int n)
{
printf("cac phan tu trong mang la:\n");
for(int i=0; i<n; i++)
{
printf("%d ", a[i]);
}
printf("\n");
}
void sapxep(int a[], int n)
{
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (a[j] > a[j + 1]) {
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}

}
int main()
{
int a[100];
sophantu();
nhapvao(a, n);
hienthi(a, n);
sapxep(a, n);
printf("cac phan tu trong mang sau khi sap xep la:\n");
for(int i=0; i<n; i++)
{
printf("%d ", a[i]);
}
}
Bài 5:
#include <stdio.h>

void readMatrixSize();
void readMatrixElements();
void sortMatrix(int sortMode, int isAscending);
void printMatrix();

void sortArray(float *array, int isAscending);

int SORT_ROWS = 0, SORT_COLUMN = 1;

int main() {
readMatrixSize();
readMatrixElements();
printf("mang da nhap la:\n");
printMatrix();
sortMatrix(SORT_ROWS, 1);
sortMatrix(SORT_COLUMN, 0);
printf("mang sau khi sap xep la:\n");
printMatrix();
}

int m, n;
float matrix[50][50];

void readMatrixSize() {
printf("nhap vao so hang, so cot: ");
scanf("%d %d", &m, &n);
}

void readMatrixElements() {
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
{
printf("nhap du lieu o vi tri [%d,%d]: ", i, j);
scanf("%f", &matrix[i][j]);
}
}

void sortMatrix(int sortMode, int isAscending){


float array[m * n];
int index = 0;
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++){
array[index] = matrix[i][j];
index++;
}
index = 0;

sortArray(array, isAscending);

for (int i = 0; i < m; i++)


for (int j = 0; j < n; j++){
if(sortMode == SORT_ROWS){
matrix[i][j] = array[index];
}else{
matrix[j][i] = array[index];
}
index++;
}
}

void printMatrix(){
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++)
{
printf("%f ", matrix[i][j]);
}
printf("\n");
}
}

void sortArray(float *array, int isAscending) {


for (int i = 0; i < m*n; i++)
for (int j = 0; j < m*n; j++)
if(isAscending ? array[i] < array[j]
: array[i] > array[j]){
float temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}

You might also like