vòng lặp for
program dfds;
uses crt;
var i,n,m:integer;
begin
clrscr;
write('n=');
readln(n);
for i:=1 to n do m=m+i;
write('gia tri cua day so M la ',' ',m);
readln;
end.
var n,i,s:integer;
begin
write('n=');readln(n);
s:=0;
for i:=1 to n do s:=s+i;
write(s);
readln;
end.
var n,a,b:integer;
begin
write('n=');readln(n);
a:=0;
b:=0;
repeat
a:=a+1;
b:=b+a;
until a=n;
write(b);
readln;
end.
a) For....do
Var M : longint;
i, n : Integer;
Begin
Writeln(' Nhap n = ');
Readln (n);
M :=0;
For i := 1 to N do M = M + i;
Writeln('M = ',M);
Readln;
End.
__________________________
b) While.....do
Var M : longint;
i, n : Integer;
Begin
Writeln(' Nhap n = ');
Readln (n);
M:=0;
i:=1;
While i <= n do begin
M := M + i;
i := i + 1;
end;
Writeln(' M = ',M);
Readln;
End.