BT lệnh while...do

Chia sẻ bởi Nguyễn Thị Chinh | Ngày 14/10/2018 | 43

Chia sẻ tài liệu: BT lệnh while...do thuộc Tin học 8

Nội dung tài liệu:

1. Viết chương trình tính S=1+2+3+…+n
program tong_1;
var n,s,i:integer;
begin
write(`nhap n= `);readln(n);
s:=0;
i:=1;
while i<=n do
begin
s:=s+i;
i:=i+1;
end;
write(`tong`,n:4,`so nguyen duong dau tien la`,s:8);
readln;
end.
2. Viết chương trình tính S=1-2+3-…+/-n
program tong;
var n,s,i:integer;
begin
write(`nhap n= `);readln(n);
s:=0;
i:=1;
while i<=n do
begin
for i:=1 to n do
if i mod 2=0 then s:=s-i else s:=s+i;
i:=i+1;
end;
write(`tong la`,s:8);
readln;
end.
3. Viết chương trình tính S=2+4+6+…+n
program tong;
var n,s,i:integer;
begin
write(`nhap n= `);readln(n);
s:=0;
i:=1;
while i<=n do
begin
for i:=1 to n do
if i mod 2=0 then s:=s+i;
i:=i+1;
end;
write(`tong la`,s:8);
readln;
end.

4. Viết chương trình tính S=1+3+5+...+n
program tong;
var n,s,i:integer;
begin
write(`nhap n= `);readln(n);
s:=0;
i:=1;
while i<=n do
begin
for i:=1 to n do
if i mod 2<>0 then s:=s+i;
i:=i+1;
end;
write(`tong la`,s:8);
readln;
end.

5. Viết chương trình tính S=1+1/2+1/3+…+1/n
program tong_1;
var n,i:integer;
s:real;
begin
write(`nhap n= `);readln(n);
s:=0;
i:=1;
while i<=n do
begin
s:=s+1/i;
i:=i+1;
end;
write(`tong la`,s:8:1);
readln;
end.
6. Viết chương trình tính S=1+1/3+1/5+...+1/n
program tong;
var n,i:integer;
s:real;
begin
write(`nhap n= `);readln(n);
s:=0;
i:=1;
while i<=n do
begin
for i:=1 to n do
if i mod 2<>0 then s:=s+1/i;
i:=i+1;
end;
write(`tong la`,s:8:1);
readln;
end.
7. Viết chương trình tính S= 1/2+1/4+..+1/n
program tong;
var n,i:integer;
s:real;
begin
write(`nhap n= `);readln(n);
s:=0;
i:=1;
while i<=n do
begin
for i:=1 to n do
if i mod 2=0 then s:=s+1/i;
i:=i+1;
end;
write(`tong la`,s:8:1);
readln;
end.
8. Viết chương trình tính S=1-1/2+1/3-1/4+…+/-1/n
program tong;
var n,i:integer;
s:real;
begin
write(`nhap n= `);readln(n);
s:=0;
i:=1;
while i<=n do
begin
for i:=1 to n do
if i mod 2=0 then s:=s-1/i else s:=s+1/i;
i:=i+1;
end;
write(`tong la`,s:8);
readln;
end.

9. Viết chương trình p=1.2.3…n
program tich;
var i,n:integer;
p:longint;
begin
write(`nhap n= `);readln(n);
p:=1;
i:=1;
while i<=n do
* Một số tài liệu cũ có thể bị lỗi font khi hiển thị do dùng bộ mã không phải Unikey ...

Người chia sẻ: Nguyễn Thị Chinh
Dung lượng: 6,41KB| Lượt tài: 1
Loại file: rar
Nguồn : Chưa rõ
(Tài liệu chưa được thẩm định)