sử dụng câu While...do và for...do hãy viết pt tính tổng sau: S= 1/2 + 1/2.3 + 1/3.4 +....+ 1/n(n+1)
2 câu trả lời
Dùng while-do:
rogram oken;
uses crt;
var n,i: integer;
s: real;
begin
clrscr;
write('nhap n: '); readln(n);
i:=1;
while i<=n do
begin
s:=s+ 1/(i*(i+1));
i:=i+1;
end;
writeln('Tong S: ',s:0:3);
readln;
end.
Dùng For-do:
program oken;
uses crt;
var n,i: integer;
s: real;
begin
clrscr;
write('nhap n: '); readln(n);
for i:=1 to n do
s:=s + 1/(i*(i+1));
writeln('Tong S: ',s:0:3);
readln;
end.
${Oken}$
While do:
program whiledo;
uses crt;
var S:integer;
i,n:integer;
begin clrscr;
writeln('Nhap 1 so n: '); readln(n);
S:=0; i:=1;
while i<=n do
begin
S:=S+1/i*(i+1); i:=i+1;
end;
write('Tong S la: ',S);
readln;
end.
For do:
program fordo;
uses crt;
var S:integer;
i,n:integer;
begin clrscr;
writeln('Nhap 1 so n: '); readln(n);
S:=0;
while i:=1 to n do
S:=S+1/i*(i+1);
write('Tong S la: ',S);
readln;
end.
Học tốt!