[ create a new paste ] login | about

Link: http://codepad.org/qfb0n9is    [ raw code | output | fork ]

C, pasted on Apr 26:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define MAX 100 

void NhapMang(int[], int);
void XuatMang(int[], int);
int KiemTraSoChan(int[], int );
int KiemTraNguyenTo( int );
int KiemTraMangNguyenTo(int[], int n);
int KiemTraTangDan(int, int);

void NhapMang(int a[], int n)
{
	for (int i = 0; i < n; i++)
	{
		printf("\nNhap vao a[%d] = ", i);
		scanf_s("%d", &a[i]);
	}
}
void XuatMang(int a[], int n)
{
	for (int i = 0; i < n; i++)
	{
		printf("%4d", a[i]);
	}
}
int KiemTraSoChan(int a[],int n)
{
	for (int i = 0; i < n; i++)
	{
		if (a[i] % 2 != 0)
		{
			return 0;
		}
	}
	return 1;
}
int KiemTraNguyenTo(int n)
{
       if (n < 2)
		{
			return 0;
		}
		else if (n > 2)
		{
			if (n % 2 == 0)
			{
				return 0;
			}
		}
		for (int i = 3; i <= sqrt((double)n); i += 2)
		{
			if (n % i == 0)
			{
				return 0;
			}
		}
	return 1;
}
int KiemTraMangNguyenTo(int a[], int n)
{
	for (int i = 0; i < n; i++)
	{
		if (KiemTraNguyenTo(a[i]) == 0)
		{
			return 0;
		}
	}
	return 1;
}
int KiemTraTangDan(int a[], int n)
{
	
	
	for (int i = 0; i < n - 1; i++)
	{ 
	    if (a[i + 1] < a[i])
		{
			return  0;
			break;
		}
		else
		{
			a[i] = a[i + 1];  // Cập nhật lại chữ số cho lần so sánh tiếp theo
		}
	
}
	return 1;
}
int main()
{
	int n;
	do
	{
		printf("\nNhap vao so luong phan tu cua mang ");
		scanf_s("%d", &n);

		if (n < 0 || n > MAX)
		{
			printf("\nPhan tu nhap vao khong hop le. Xin vui long kiem tra lai !");
		}


	} while (n < 0 || n > MAX);

	int a[MAX];
	NhapMang(a, n);
	XuatMang(a, n);
	
	// Câu a
	int Check = KiemTraSoChan(a, n);
	if (Check == 0)
	{
		printf("\nTon tai so le trong mang !");
	}
	else
	{
		printf("\nTat cac cac so trong mang la so chan !");
	}

	// Câu b
	int CheckNguyenTo = KiemTraMangNguyenTo(a,n);
	if (CheckNguyenTo == 0)
	{
		printf("\nTon tai it nhat 1 so khong phai so nguyen to trong mang !");
	}
	else
	{
		printf("\nTat cac cac so trong mang la so nguyen to !");
	}


	// Câu c
	int CheckTangDan = KiemTraTangDan(a, n);
	if (CheckTangDan == 0)
	{
		printf("\nMang khong tang dan !");
	}
	else
	{
		printf("\nMang tang dan !");
	}
	_getch();
}


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Line 17: error: conio.h: No such file or directory
In function 'NhapMang':
Line 15: error: 'for' loop initial declaration used outside C99 mode
In function 'XuatMang':
Line 23: error: 'for' loop initial declaration used outside C99 mode
In function 'KiemTraSoChan':
Line 30: error: 'for' loop initial declaration used outside C99 mode
In function 'KiemTraNguyenTo':
Line 52: error: 'for' loop initial declaration used outside C99 mode
In function 'KiemTraMangNguyenTo':
Line 63: error: 'for' loop initial declaration used outside C99 mode
t.c: At top level:
Line 73: error: conflicting types for 'KiemTraTangDan'
Line 11: error: previous declaration of 'KiemTraTangDan' was here
In function 'KiemTraTangDan':
Line 76: error: 'for' loop initial declaration used outside C99 mode


Create a new paste based on this one


Comments: