cho tệp SONGUYEN.INP, chứa một dãy số nguyên bất kì:
Viết chương trình pascal: Tính tổng các số chẵn trong dãy số nguyên đó. kết quả ghi vào tệp TONG.OUT.
Cho tệp SONGUYEN.INT, chứa dãy số nguyên bất kì. Viết công thức Pascal : Đếm các số chẵn trong dãy số nguyên đó. Kết quả ghi vào tệp TONG.OUT Giúp em với ạ😔
const fi='songuyen.inp'
fo='tong.out'
var f1,f2:text;
a:array[1..100]of integer;
n,i,dem:integer;
begin
assign(f1,fi); reset(f1);
assign(f2,fo); rewrite(f2);
n:=0;
while not eof(f1) do
begin
n:=n+1;
read(f1,a[n]);
end;
dem:=0;
for i:=1 to n do
if a[i] mod 2=0 then inc(dem);
writeln(f2,dem);
close(f1);
close(f2);
end.
Cho tệp SONGUYEN.INP chứa dãy số nguyên bất kì. Viết ct tính tổng các số chẵn trong dãy và in kết quả ra mh.
uses crt;
const fi='songuyen.inp'
var f1:text;
a:array[1..100]of integer;
i,n,t:integer;
begin
clrscr;
assign(f1,fi); reset(f1);
n:=0;
while not eoln(f1) do
begin
n:=n+1;
read(f1,a[n]);
end;
t:=0;
for i:=1 to n do
if a[i] mod 2=0 then t:=t+a[i];
writeln(t);
readln;
end.
Cho tệp 'dayso.in' chứa các dãy số nguyên A1, A2....,An mỗi dãy sô nằm trên 1 dòng. Viết chương trình tính tổng theo dòng trong tệp 'dayso.in' sau đó ghi các số tổng này lên tệp 'tong.out', mỗi số ghi trên một dòng
cho tệp songuyen.inp chứa dãy các số nguyên ( mỗi số cách nhau một dấu cách). Đọc dữ liệu từ tệp đã cho và
a) Tính tổng các số lẻ trong tệp và đưa kết quả lên màn hình.
b) tính tổng các số lẻ trong tệp và ghi kết quả vào tệp songuyen2
c) đưa ra màn hình các số lẻ trong tệp
uses crt;
const fi='songuyen.inp'
fo='songuyen2.out'
var a:array[1..100]of integer;
i,n,t1:integer;
f1,f2:text;
begin
clrscr;
assign(f1,fi); reset(f1);
assign(f2,fo); rewrite(f2);
readln(f1,n);
for i:=1 to n do readln(f1,a[i]);
t1:=0;
for i:=1 to n do if a[i] mod 2<>0 then t1:=t1+a[i];
writeln(t1);
writeln(f2,t1);
for i:=1 to n do
if a[i] mod 2<>0 then write(a[i]:4);
close(f1);
close(f2);
readln;
end.
Cho tệp bai1.inp gồm:
Hàng 1:Chứa số nguyên N
Hàng 2:Chứa dãy số nguyên gồm N chữ số
Yêu cầu:-Tính tổng các số lẻ và đếm các số chẵn của dãy số trên
-Ghi kết quả tổng và đếm vào tệp bai1.out
const fi='bai1.inp'
fo='bai1.out'
var f1,f2:text;
a:array[1..100]of integer;
n,i,t,dem:integer;
begin
assign(f1,fi); reset(f1);
assign(f2,fo); rewrite(f2);
readln(f1,n);
for i:=1 to n do
read(f1,a[i]);
t:=0;
dem:=0;
for i:=1 to n do
begin
if a[i] mod 2<>0 then t:=t+a[i]
else inc(dem);
end;
writeln(f2,'Tong cac so le la: ',t);
writeln(f2,'So so chan la: ',dem);
close(f1);
close(f2);
end.
Viết chương trình và sử dụng chương trình con đọc dữ liệu từ tệp input.txt chứa dãy số nguyên trên một dòng. Các số cách nhau một khoảng trắng
Tính tổng dãy số trong tập input.txt rồi ghi kết quả tổng này vào tập output.txt
Giúp em với ạ. Em cảm ơn
const fi='input.txt'
fo='output.txt'
var f1,f2:text;
a:array[1..100]of integer;
i,n,t:integer;
begin
assign(f1,fi); reset(f1);
assign(f2,fo); rewrite(f2);
n:=0;
while not eof(f1) do
begin
inc(n);
read(f1,a[n]);
end;
t:=0;
for i:=1 to n do t:=t+a[i];
write(f2,t);
close(f1);
close(f2);
end.
const fi='input.txt'
fo='chiahet.txt'
var f1,f2:text;
a:array[1..100]of integer;
i,n,dem:integer;
begin
assign(f1,fi); reset(f1);
assign(f2,fo); rewrite(f2);
n:=0;
while not eoln(f1) do
begin
inc(n);
read(f1,a[n]);
end;
dem:=0;
for i:=1 to n do
if a[i] mod 3=0 then inc(dem);
writeln(f2,dem);
close(f1);
close(f2);
end.
Cho tệp DATA.INP mỗi dòng chứa 1 số nguyên . Hãy viết chương trình pascal tính trung bình cộng các số kết quả ghi vào tệp KQ.OUT
const fi='data.inp'
fo='kq.out'
var f1,f2:text;
a:array[1..100]of integer;
i,n,t:integer;
begin
assign(f1,fi); reset(f1);
assign(f2,fo); rewrite(f2);
n:=0;
while not eoln(f1) do
begin
inc(n);
read(f1,a[n]);
end;
t:=0;
for i:=1 to n do
t:=t+a[i];
writeln(f2,t/n:4:2);
close(f1);
close(f2);
end.
Viết chương trình đọc vào 2 số nguyên dương từ tệp songuyen.inp tính hiệu của hai số nguyên ghi kết quả ra tệp ketqua.out