bài 2:
ct tính tổng S= 1 + 1/3 + 1/5 + ... + 1/(2n+1)
Code:
var n,i:integer;
s:real;
BEGIN
repeat
write('nhap n nguyen duong : '); readln(n);
until n>0;
s:=1.0;
for i:=1 to n do
s:=s+ 1/(2*i+1);
writeln('tong s = ',s:5:3);
readln;
END.
ct tính tổng S = 1 - 1/2 + 1/3 - 1/4 + ... + ((-1)^(n+1))*1/n
Code:
var n,i:integer;
s:real;
BEGIN
repeat
write('nhap n nguyen duong : '); readln(n);
until n>0;
s:=0.0;
for i:=1 to n do
begin
if (i mod 2 = 1) then s:=s+ 1/i
else s:= s -1/i;
end;
writeln('tong s = ',s:5:3);
readln;
END.
Bookmarks