[ create a new paste ] login | about

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

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

void NhapDuLieu(int &n)
{
	do
	{
		printf("\nNhap vao n (n >= 0) ");
		scanf_s("%d", &n);
		if (n < 0)
		{
			printf("\nGia tri n khong hop le. Xin kiem tra lai !");
		}
	} while (n < 0);
}

int TraVeSoDao(int n)
{
	int SoNghichDao = 0;
	while (n != 0)
	{
		SoNghichDao = SoNghichDao * 10 + n % 10;
		n /= 10;
	}
	return SoNghichDao;
}

// Nếu là  số đối xứng => true , còn không => false
bool KiemTraDoiXung(int n)
{
	return n == TraVeSoDao(n);
}

bool KiemTraChinhPhuong(int n)
{
	return sqrt(float(n)) == (int)sqrt((float)n);
}
// nguyên tố => true
bool KiemTraNguyenTo(int n)
{
	if (n < 2)
	{
		return false;
	}
	else if (n > 2)
	{
		if (n % 2 == 0)
		{
			return false;
		}
		for (int i = 3; i <= sqrt((float)n); i += 2)
		{
			if (n % i == 0)
			{
				return false;
			}
		}
	}
	return true;
}
int TinhTongChuSoLe(int n)
{
	int Tong = 0;
	while (n != 0)
	{
		int chuso = n % 10;
		if (chuso % 2 != 0)   // Lấy ra chữ số rồi kiểm tra xem nó có chia hết cho 2 hay không 
		{
			Tong += chuso;
		}
		n /= 10;
	}
	return Tong;
}

int TinhTongChuSoNguyenTo(int n)
{
	int Tong = 0;
	while (n != 0)
	{
		int chuso = n % 10;
		n /= 10;
        if (KiemTraNguyenTo(chuso))
		{
			Tong += chuso;
		}
    }
	return Tong;
}

int TinhTongChinhPhuong(int n, bool &Check)
{
	Check = false;
	int Tong = 0; //	Mẹo để biết có tồn tại số chính phương nào hay không
	while (n != 0)
	{
		int chuso = n % 10;
		n /= 10;

		if (KiemTraChinhPhuong(chuso))
		{
			Check = true;
			Tong += chuso;
		}
	}
	return Tong;
}
int main()
{
	int n;
	NhapDuLieu(n);
	printf("\nn = %d", n);

	// Câu a
	int SoNghichDao = TraVeSoDao(n);
	printf("\nSo nghich dao = %d ", SoNghichDao);

	// Câu b
	bool Check = KiemTraDoiXung(n);
	if (Check == true)
	{
		printf("\nLa so doi xung ");
	}
	else
	{
		printf("\nKhong la so doi xung ");
	}

	// Câu c
	bool CheckChinhPhuong = KiemTraChinhPhuong(n);
	if (CheckChinhPhuong)
	{
		printf("\nLa so chinh phuong ");
	}
	else
    {
			printf("\nKhong phai so chinh phuong");
    }

	// Câu d
	bool CheckNguyenTo = KiemTraNguyenTo(n);
	if (CheckNguyenTo)
	{
		printf("\nLa so nguyen to ");
	}
	else
	{
		printf("\nKhong phai so nguyen to ");
	}

	// Câu e
	int TongChuSoLe = TinhTongChuSoLe(n);
	if (TongChuSoLe == 0)
	{
		printf("\nKhong co ton tai chu so le ben trong ");
	}
	else
	{
		printf("\nTong cac chu so le la: %d", TongChuSoLe);
	}
	
	// Câu f
	int TongChuSoNguyenTo = TinhTongChuSoNguyenTo(n);
	if (TongChuSoNguyenTo == 0)
	{
		printf("\nKhong co ton tai chu so nguyen to ben trong");
	}
	else
	{
		printf("\nTong cac chu so nguyen to la: %d", TongChuSoNguyenTo);
	}

	// Câu g
	//int TongChuSoChinhPhuong = TinhTongChinhPhuong(n);
	//if (TongChuSoChinhPhuong == 0)
	//{
	//	printf("\nKhong co ton tai chu so chinh phuong ben trong ");
	//}
	//else
	//{
	//	  // Cộng bù lại
	//	printf("\nTong cac chu so chinh phuong la: %d", TongChuSoChinhPhuong);
	//}

	bool ChechTonTai;
	int TongChinhPhuong = TinhTongChinhPhuong(n, ChechTonTai);

	if (!ChechTonTai)
	{
		printf("\nKhong co ton tai chu so chinh phuong ben trong ");
	}
	else
	{
		printf("\nTong cac chu so chinh phuong la: %d", TongChinhPhuong);
	}

	_getch();
	return 0;
}


Output:
Line 17: error: conio.h: No such file or directory
Line 5: error: expected ';', ',' or ')' before '&' token
Line 30: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'KiemTraDoiXung'
Line 35: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'KiemTraChinhPhuong'
Line 40: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'KiemTraNguyenTo'
Line 92: error: expected declaration specifiers or '...' before 'bool'
In function 'TinhTongChinhPhuong':
Line 94: error: 'Check' undeclared (first use in this function)
Line 94: error: (Each undeclared identifier is reported only once
Line 94: error: for each function it appears in.)
Line 94: error: 'false' undeclared (first use in this function)
Line 103: error: 'true' undeclared (first use in this function)
In function 'main':
Line 120: error: 'bool' undeclared (first use in this function)
Line 120: error: expected ';' before 'Check'
Line 121: error: 'Check' undeclared (first use in this function)
Line 121: error: 'true' undeclared (first use in this function)
Line 131: error: expected ';' before 'CheckChinhPhuong'
Line 132: error: 'CheckChinhPhuong' undeclared (first use in this function)
Line 142: error: expected ';' before 'CheckNguyenTo'
Line 143: error: 'CheckNguyenTo' undeclared (first use in this function)
Line 186: error: expected ';' before 'ChechTonTai'
Line 187: error: 'ChechTonTai' undeclared (first use in this function)
Line 187: error: too many arguments to function 'TinhTongChinhPhuong'


Create a new paste based on this one


Comments: