Borland Pascal Wiki
Advertisement
 This script page was blocked. The script was reviewed and it works perfectly so this page doesn't need anymore editing.

This script adds a number range (1+2+3+4+5+...+n) using FOR...DO:

Variant 1

var x,s,i:integer;
begin
 write ('n='); readln (n);
 for i:=1 to n do
 s:=s+i:
 write ('The sum is: ',s);
 readln;
end.

Explanation

For the first, we read n from the keyboard. Then, in order to make a number range,we'll use FOR...DO. So i will be for the first 1, then 2, then 3,... We only need to add i to the sum s and show it on the screen.

Variant 2

var x,s,i:integer;
begin
 write ('n='); readln (n);
 i:=0; {initialization}
 repeat
 i:=i+1;
 s:=s+i;
 until i=n;
 write ('The sum is: ',s);
 readln;
end.

Explanation

For the first, we read n from the keyboard. Then, in order to make a number range,we'll use REPEAT...UNTIL. So i will be for the first 1, then 2, then 3,... and the REPEAT will break the cycle if i will become equal to n We only need to add i to the sum s and show it on the screen.

See also

Advertisement