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 .
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.
Chương trình C++
Nhập từ bản phim dãy gồm n số nguyên (n<=100).
* In ra màn hình dãy các số chẵn trong dãy đã nhập
* In ra màn hình giá trị trung bình cộng của dây các số chân từ dãy đã nhập
• In ra màn hình tổng các số lẻ và chia hết cho 3
#include <bits/stdc++.h>
using namespace std;
long long n,i,a[1000],t,dem,t1;
int main()
{
cin>>n;
for (i=1; i<=n; i++) cin>>a[i];
for (i=1; i<=n; i++)
if (a[i]%2==0) cout<<a[i]<<" ";
cout<<endl;
t=0;
dem=0;
for (i=1; i<=n; i++)
if (a[i]%2==0)
{
t=t+a[i];
dem++;
}
cout<<fixed<<setprecision(2)<<(t*1.0)/(dem*1.0)<<endl;
t1=0;
for (i=1; i<=n; i++)
if (a[i]%2!=0 || a[i]%3==0) t1+=a[i];
cout<<t1;
return 0;
}
Viết chương trình nhập vào một dãy số, chương trình chỉ dừng lại khi số nhập vào là số 0. In ra màn hình các kết quả sau: - In ra tổng của các số chẵn đã nhập vào? - In ra tích của các số lẻ đã nhập vào?
Giúp tui với mn ơi
uses crt;
var n,t1:integer;
s:real;
begin
clrscr;
t1:=0;
s:=1;
repeat
readln\(\left(n\right)\);
if n mod 2=0 then t1:=t1+n
else s:=s*n;
until n=0;
writeln\(\left(t1\right)\);
writeln\(\left(s:4:2\right)\);
readln;
end.
Câu 1 : Viết chương trình nhập vào dãy số gồm n phần tử. Tính và in ra màn hình tổng các số chẵn, các số lẻ của dãy vừa nhập
program TongChanLe;
var
n,i, chans, les: integer; a: array[1..100] of integer;
begin
chans := 0;
les := 0;
write('Nhap n: ');
readln(n);
for i:=1 to n do
begin
write('Nhap so thu ', i, ': ');
readln(a[i]);
if a[i] mod 2 = 0 then
chans := chans + a[i]
else
les := les + a[i];
end;
writeln('Tong cac so chan la: ', chans);
writeln('Tong cac so le la: ', les);
readln;
end.
1 Nhập dãy số nguyên gồm n phần tử viết chương trình in ra những số có giá trị lẻ 2 nhập vào dãy số nguyên gồm n phần tử viết chương trình tính tổng các số chẵn có trong mảng 3 nhập vào dãy số nguyên gồm n phần tử viết chương trình tính tổng các số lẻ có trong mảng
Bài 1:
uses crt;
var a:array[1..100]of integer;
i,n: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
if a[i] mod 2<>0 then write(a[i]:4);
readln;
end.
Bài 2:
uses crt;
var a:array[1..100]of integer;
i,n: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
if a[i] mod 2=0 then write(a[i]:4);
readln;
end.
Bài 3:
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] mod 2<>0 then t:=t+a[i];
writeln(t);
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.
1/viết chương trình nhập dãy số gồm 10 số nguyên dương.In ra màn hình số bé nhất (Min)
2/viết chương trình nhập dãy số gồm n số nguyên.
a. Tính tổng các phần tử của dãy.
b. In ra màn hình các phần tử là số chẵn hoặc lẻ.
[Giúp mình với mình đang cần gấp mai là mình thì rồi. Cảm ơn mn trước ạ]
1:
#include <bits/stdc++.h>
using namespace std;
long long n=10, a[10],i,nn;
int main()
{
for (i=1; i<=n; i++) cin>>a[i];
nn=a[1];
for (i=2; i<=n; i++) nn=min(nn,a[i]);
cout<<nn;
return 0;
}
Nhập vào một dãy số nguyên gồm n phần tử. Viết chương trình In ra màn hình tổng các số lẽ, số chẵn của dãy?
Var a:array[1..1000] of integer;
i,n:integer;
sc,sl:longint;
Begin
Write('Nhap so luong so cua day ');readln(n);
For i:=1 to n do
Begin
Write(a,'[',i,'] = ');readln(a[i]);
End;
Write('Tong cac so chan la ');
For i:=1 to n do
If a[i] mod 2 = 0 then sc:=sc+a[i];
Writeln(sc);
Write('Tong cac so le la ');
For i:=1 to n do
If a[i] mod 2 <> 0 then sl:=sl+a[i];
Write(sl);
Readln
End.
Viết chương trình nhập vào dãy n số nguyên ( n < 150 ) hãy in ra màn hình dãy vừa nhập và tính tổng các phần tử lẻ của dãy
n = int(input('Nhập n ( n < 150): '))
if n >= 150:
print('n phải nhỏ hơn 150')
else:
arr = [ ]
total = 0
# Nhập vào dãy số nguyên
for i in range(n):
arr.append(int(input('Nhập phần tử thứ ' + str(i+1) + ': ')))
# In dãy vừa nhập
print('Dãy vừa nhập là: ', end='')
for i in range(n):
print(arr[i], end=' ')
# Tính tổng các phần tử lẻ
for i in range(n):
if arr[i] % 2 != 0:
total += arr[i]
print('\nTổng các phần tử lẻ là:', total)