Câu 1 Viết chương trình:
a.Nhập vào mảng A là dãy số nguyên
b.In ra màn hình dãy số A đó
c.In ra màn hình các số nguyên dương trong dãy số A
d.Tính tổng các số lẻ trong dãy số A và hiện kết quả ra màn hình
Viết chương trình:
a. Nhập vào mảng A là dãy số nguyên
b. In ra hình dãy số A đó
c. In ra màn hình các số nguyên dương trong dãy số A
d. Tính tổng các số lẽ trong dãy lập trình pascal ta có câu lệnh
Bài 1: Viết chương trình nhập vào 1 dãy số nguyên xuất dãy số đó ra màn hình tính tích các số chẵn và xuất ra màn hình.
Bài 2: Viết chương trình nhập 1 dãy số nguyên, xuất dãy số đó ra màn hình tính tổng các số chẵn các số lẻ và xuất ra màn hình.
program timtich;
uses crt;
var i,n:integer;
tich:longint;
a:array[1..100]of integer;
begin
clrscr;
write('nhap so n:');readln(n);
for i:=1 to n do
begin
write('nhap phan tu a[',i,']:');readln(a[i]);
end;
for i:=1 to n do
write(a[i]:4);
tich:=1;
writeln;
for i:=1 to n do
if a[i] mod 2=0 then tich:=tich*a[i];
writeln('tich ca phan tu chan cua mang la:',tich);
readln;
end.
program timtong;
uses crt;
var i,n:integer;
tong:longint;
a:array[1..100]of integer;
begin
clrscr;
write('nhap so n:');readln(n);
for i:=1 to n do
begin
write('nhap phan tu a[',i,']:');readln(a[i]);
end;
for i:=1 to n do
write(a[i]:4);
tong:=0;
writeln;
for i:=1 to n do
if a[i] mod 2=0 then tong:=tong+a[i];
writeln('tong cac phan tu chan cua mang la:',tong);
tong:=0;
writeln;
for i:=1 to n do
if a[i] mod 2=1 then tong:=tong+a[i];
writeln('tong cac phan tu le cua mang la:',tong);
readln;
end.
Bài 1:
uses crt;
var a:array[1..100]of integer;
i,n,s:integer;
begin
clrscr;
write('Nhap n='); readln(n);
for i:=1 to n do
begin
write('A[',i,']='); readln(a[i]);
end;
s:=1;
for i:=1 to n do
if a[i] mod 2=0 then s:=s*a[i];
writeln(s);
readln;
end.
uses crt;
var a:array[1..100]of integer;
i,n,t1,t2:integer;
begin
clrscr;
write('Nhap n='); readln(n);
for i:=1 to n do
begin
write('A[',i,']='); readln(a[i]);
end;
t1:=0;
t2:=0;
for i:=1 to n do
begin
if a[i] mod 2=0 then t1:=t1+a[i]
else t2:=t2+a[i];
end;
writeln('Tong cac so chan la: ',t1);
writeln('Tong cac so le la: ',t2);
readln;
end.
Viết chương trình Nhập dãy số nguyên từ bàn phím. In ra màn hình các số lẻ có trong dãy được nhận. Tính tổng các số có trong dãy và in ra màn hình
Python:
n = input("Nhập dãy số nguyên: ").split()
n = [int(i) for i in n]
so_le = [i for i in n if i % 2 != 0]
tong = sum(n)
print("Các số lẻ trong dãy là: ", so_le)
print("Tổng các số trong dãy là: ", tong)
Pascal:
program tongvasole;
const
MAX_SIZE = 1000;
var
numbers: array[1..MAX_SIZE] of Integer;
count, i: Integer;
total: Integer;
begin
Write('Nhập số lượng phần tử trong dãy: ');
ReadLn(count);
for i := 1 to count do
begin
Write('Nhập phần tử thứ ', i, ': ');
ReadLn(numbers[i]);
end;
Write('Các số lẻ trong dãy là: ');
total := 0;
for i := 1 to count do
begin
if numbers[i] mod 2 <> 0 then
Write(numbers[i], ' ');
total := total + numbers[i];
end;
WriteLn;
WriteLn('Tổng các số trong dãy là: ', total);
end.
sử dụng biến mảng viết chương trình nhập vào 1 dãy số gồm n số nguyên từ bàn phím . In ra màn hình tổng các số dương trong dãy số đó
Var a:array[1..1000] of integer;
s:longint;
i,n:integer;
Begin
Write('n = ');readln(n);
For i:=1 to n do
Begin
Write('Nhap so thu ',i);readln(a[i]);
If a[i] > 0 then s:=s+a[i];
End;
Write('Tong la ',s);
Readln
End.
Sử dụng biến mảng viết chương trình nhập vào một dãy số gồm N số nguyên tử bàn phím in ra màn hình tổng các số dương trong dãy số đó.
uses crt;
var a:array[1..100]of integer;
i,n,t:integer;
begin
clrscr;
write('Nhap n='); readln(n);
for i:=1 to n do
begin
write('A[',i,']='); readln(a[i]);
end;
t:=0;
for i:=1 to n do
if a[i]>0 then t:=t+a[i];
writeln(t);
readln;
end.
Viết chương trình nhập vào từ bàn phím mảng B gồm N số nguyên (N<=200)và 1 số nguyên dương k:
1. Tính tổng các phần tử lẻ trong mảng A và đưa kết quả ra màn hình?
2. Tính tổng các phần tử là bội của K trong mảng A và đưa kết quả ra màn hình?
uses crt;
var a:array[1..200]of integer;
n,i,k,t,t1:integer;
begin
clrscr;
write('Nhap n='); readln(n);
for i:=1 to n do
begin
write('A[',i,']='); readln(a[i]);
end;
write('Nhap k='); readln(k);
t:=0;
for i:=1 to n do
if a[i] mod 2<>0 then t:=t+a[i];
t1:=0;
for i:=1 to n do
if a[i] mod k=0 then t1:=t1+a[i];
writeln('Tong cac phan tu le la: ',t);
writeln('Tong cac phan tu la boi cua ',k,' la: ',t1);
readln;
end.
sử dụng biến mảng Viết chương trình nhập vào dãy số gồm n số nguyên từ bàn phím in ra màn hình tổng các số dương trong dãy số đó Giúp mình với
uses crt;
var a:array[1..100]of integer;
i,n,t:integer;
begin
clrscr;
write('Nhap n='); readln(n);
for i:=1 to n do
begin
write('A[',i,']='); readln(a[i]);
end;
t:=0;
for i:=1 to n do
if a[i]>0 then t:=t+a[i];
writeln(t);
readln;
end.
viết chương trình nhập từ bàn phím số nguyên dương n và dãy số nguyên a1 a2 đến an đưa dãy vừa nhập ra màn hình tính tổng các số chia hết cho 7 đưa kết quả ra màn hình
uses crt;
var a:array[1..100]of integer;
i,n,t:integer;
begin
clrscr;
write('Nhap n='); readln(n);
for i:=1 to n do
begin
write('A[',i,']='); readln(a[i]);
end;
for i:=1 to n do
write(a[i]:4);
writeln;
t:=0;
for i:=1 to n do
if a[i] mod 7=0 then t:=t+a[i];
writeln('Tong cac so chia het cho 7 la: ',t);
readln;
end.
Viết chương trình nhập vào 1 dãy số nguyên . In ra màn hình tổng các chẵn , tổng các số lẻ có trong dãy đã cho .
Program HOC24;
var i,n: integer;
t1,t2: longint;
begin
write('Nhap so phan tu cua day: '); readln(a[i]);
for i:=1 to n do
begin
write('Nhap phan tu thu ',i,': '); readln(a[i]);
end;
t1:=0; t2:=0;
for i:=1 to n do if a[i] mod 2=0 then t1:=t1+a[i] else t2:=t2+a[i];
writeln('Tong cac so chan la: ',t1);
write('Tong cac so le la: ',t2);
readln
end.