Viết chương trình:
Đọc từ tệp “DULIEU.TXT” 2 số nguyên M và N (M < N)
Tính tổng các số chia hết cho 3 trong phạm vi từ M đến N
Ghi kết quả ra tệp “KETQUA.TXT”
Var f1, f2: text;
Tong, m, n, i: integer;
Begin
assign(f1, ‘DULIEU.TXT’);
reset(f1);
assign(f2, ‘KETQUA’);
rewrite(f2);
Tong;= 0;
read(f1, m, n);
for i:= m to n do
if i mod 2 = 0 then Tong:= Tong+i;
write(f2, Tong);
close(f1);
close(f2);
End.
Viết chương trình:
Đọc từ tệp “DULIEU.TXT” 2 số nguyên M và N (M < N)
Tính tổng các số chia hết cho 5 trong phạm vi từ M đến N
Ghi kết quả ra tệp “KETQUA.TXT”.
Var f1, f2: text;
Tong, m, n, i: integer;
Begin
assign(f1, ‘DULIEU.TXT’);
reset(f1);
assign(f2, ‘KETQUA’);
rewrite(f2);
Tong;= 0;
read(f1, m, n);
for i:= m to n do
if i mod 2 = 0 then Tong:= Tong+i;
write(f2, Tong);
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.
tệp data inp chứa một dãy số gồm 10 số cách nhau bởi dấu cách em hãy viết chương trình đọc toàn bộ các chữ số trong tệp tính và ghi kết quả vào tệp data.out tổng các chữ số chia hết cho 5
Cho tệp Dulieu.txt chứa các số nguyên, mỗi giá trị cách nhau một khoảng trống.Đọc từ tệp ra các giá trị gắn vào 2 biến a,b. Tính tổng a+b và ghi ra kết quả vào tệp ketqua.txt Giúp em với ạ, chiều nay là deadline rồi ạ, em cảm ơn nhiều!!!
#include <bits/stdc++.h>
using namespace std;
long long a,b;
int main()
{
freopen("dulieu.txt","r",stdout);
freopen("ketqua.txt","w",stdout);
cin>>a>>b;
cout<<a+b;
return 0;
}
const fi='dulieu.txt'
fo='ketqua.txt'
var f1,f2:text;
a,b,c,d:array[1..100]of integer;
i,n,dem,dem1:integer;
begin
assign(f1,fi); reset(f1);
assign(f2,fo); rewrite(f2);
n:=0;
while not eof(f1) do
begin
n:=n+1;
readln(f1,a[n],b[n]);
end;
dem:=0;
dem1:=0;
for i:=1 to n do
if a[i]>b[i] then
begin
inc(dem);
inc(dem1);
c[dem]:=a[i];
d[dem]:=b[i];
end
else begin
inc(dem);
inc(dem1);
c[dem]:=b[i];
d[dem]:=a[i];
end;
for i:=1 to dem do
write(f2,c[i]:4);
writeln(f2);
for i:=1 to dem1 do
write(f2,d[i]:4);
close(f1);
close(f2);
end.
Cho tệp số nguyên Dl.txt chứa các số nguyên , mỗi số cách nhau một kí tự trắng .Viết chương trình đọc các số nguyên dương từ tệp rồi đưa ra tổng các số dương và ghi vào tệp soam.txt các số nguyên âm .
uses crt;
const fi='dl.txt'
fo='soam.txt'
var f1,f2:text;
a:array[1..100]of integer;
i,n:integer;
begin
clrscr;
assign(f1,fi); reset(f1);
assign(f2,fo); rewrite(f2);
n:=0;
while not eoln(f1) do
begin
n:=n+1;
read(f1,a[n]);
end;
for i:=1 to n do
begin
if a[i]>0 then writeln('Can bac hai cua ',a[i],' la: ',sqrt(a[i]):4:2)
else if a[i]<0 then write(f2,a[i]:4);
end;
close(f1);
close(f2);
readln;
end.
Cho tệp số nguyên Dl.txt chứa các số nguyên , mỗi số cách nhau một kí tự trắng .Viết chương trình đọc các số nguyên dương từ tệp rồi đưa ra tổng các số dương và ghi vào tệp soam.txt các số nguyên âm .
uses crt;
const fi='dl.txt'
fo='soam.txt'
var f1,f2:text;
a:array[1..100]of integer;
i,n:integer;
begin
clrscr;
assign(f1,fi); reset(f1);
assign(f2,fo); rewrite(f2);
n:=0;
while not eoln(f1) do
begin
n:=n+1;
read(f1,a[n]);
end;
for i:=1 to n do
begin
if a[i]>0 then writeln('Can bac hai cua ',a[i],' la: ',sqrt(a[i]):4:2)
else if a[i]<0 then write(f2,a[i]:4);
end;
close(f1);
close(f2);
readln;
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.