[ create a new paste ] login | about

Link: http://codepad.org/FILkDdum    [ raw code | output | fork | 1 comment ]

C, pasted on Sep 14:
#include<stdio.h>
#include<conio.h>
#include<math.h>
/*
	Số chính phương là số mà kết quả khai căn bậc 2 là 1 số nguyên
	sqrt(4) = 2.00000 => ép về nguyên = 2 => vì 2.000  == 2 (true) => là số chính phương
	sqrt(5) = 2.23234 => ép về nguyên = 2 => vì 2.4324 != 2 (false) => không là số chính phương

	*/
bool KiemTraChinhPhuong(int n)
{
	return sqrt(float(n)) == (int)sqrt((float)n);
}

void LietKeChinhPhuong(int n)
{
	for(int i = 2; i < n; i++)
	{
		if(KiemTraChinhPhuong(i) == true)
			printf("%4d", i);
	}
}
int main()
{
	int n;
	printf("\nNhap n: ");
	scanf("%d", &n);

	LietKeChinhPhuong(n);

	getch();
	return 0;
}


Output:
1
2
3
4
5
6
7
Line 17: error: conio.h: No such file or directory
Line 10: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'KiemTraChinhPhuong'
In function 'LietKeChinhPhuong':
Line 17: error: 'for' loop initial declaration used outside C99 mode
Line 19: error: 'true' undeclared (first use in this function)
Line 19: error: (Each undeclared identifier is reported only once
Line 19: error: for each function it appears in.)


Create a new paste based on this one


Comments:
posted by yonnon on Aug 2
#include<bits/stdc++.h>

using namespace std;
bool check(int n)
{
if( n < 4 )
return false;
else if( n >= 4 )
{
if ( sqrt(static_cast<float>(n)) == static_cast<int>(sqrt(static_cast<float>(n))))
{
return true;
}
return false;
}
return false;
}
void lietke(int n)
{
for(int i = 4; i <= n; ++i)
{
if(check(i) == true)
cout<<" "<<i;
}
}
int main()
{
int n;
cout <<"Enter your sentence: ";
cin >> n;
lietke(n);
return 0;
}
reply