Trước hết, chúng ta sẽ tìm hiểu khái niệm về số chính phương. Số chính phương là gì? Số chính phương là một số mà tự nó là căn bậc hai của một
số tự nhiên khác, hay nói rõ hơn thì số chính phương là bình
phương của một số tự nhiên.
Ví dụ: 289 là một số chính phương vì 289 = 17 bình phương.
Ví dụ: 289 là một số chính phương vì 289 = 17 bình phương.
Thuật toán Pascal dưới đây sẽ giúp tìm số chính phương trong mảng 1 chiều.
uses crt;
type ArrInt = array[1..250] of integer;
Var n,i,x : integer;
a: ArrInt;
BEGIN
clrscr;
write('Nhap so phan tu: ');
readln(n);
for i:=1 to n do
begin
write('Phan tu thu ',i,'= ');
readln(a[i]);
end;
writeln('Cac so chinh phuong co trong mang:');
for i:=1 to n do
begin
x:=trunc(sqrt(a[i]));
if sqr(x)=a[i] then
write(a[i]:4);
end;
readln;
END.
type ArrInt = array[1..250] of integer;
Var n,i,x : integer;
a: ArrInt;
BEGIN
clrscr;
write('Nhap so phan tu: ');
readln(n);
for i:=1 to n do
begin
write('Phan tu thu ',i,'= ');
readln(a[i]);
end;
writeln('Cac so chinh phuong co trong mang:');
for i:=1 to n do
begin
x:=trunc(sqrt(a[i]));
if sqr(x)=a[i] then
write(a[i]:4);
end;
readln;
END.
Trong đó lệnh hàm sqrt để lấy căn và hàm trunc để lấy phần nguyên.
Nhãn:
Bài tập Pascal,
Mảng 1 chiều,
Pascal cơ bản
Previous Article

Hello there! I know this is kinda off topic but I was
wondering which blog platform are you using for this website?
I'm getting sick and tired of Wordpress because I've had problems with hackers and I'm looking at alternatives for another platform. I would be great if you could point me in the direction of a good platform.
Also visit my web site: cash advance franchise
Also see my webpage > payday loan no faxing
14:33 Ngày 23 tháng 2 năm 2013
If you desire to increase your experience just keep visiting this web page and be updated with the most up-to-date
news posted here.
Also visit my weblog; chapter 7 bankruptcy florida
02:02 Ngày 16 tháng 3 năm 2013
hay lắm , tks very much
19:56 Ngày 23 tháng 5 năm 2013
!!!!!!!!!!
13:36 Ngày 25 tháng 5 năm 2013
Thuật toán kiểm tra số chính phương không hay lắm.
Thay thế:
If trunc(sqrt(A[i])=sqrt(A[i]) then write(A[i]:4);
21:18 Ngày 31 tháng 5 năm 2013
Đăng nhận xét