BT1: Cho số nguyên n, dãy số a1, a2, …, an. Dùng vector a/ Tính số lượng các chẵn trong dãy. b/ tính tổng các số nguyên tố trong dãy. (ghi code c++ giúp mình với ạ)
Viết chương trình nhập từ bàn phím số nguyên dương N (N<=100) và dãy A gồm N số nguyên , A1, A2,...,An
a) Đếm số hợp số chẵn và số lẻ
b) Tính tổng các số chẵn
c) đếm số lượng nguyên tố trong dãy
uses crt;
var a:array[1..100]of integer;
i,n,tc,tl,t:integer;
begin
clrscr;
write('Nhap n='); readln(n);
for i:=1 to n do
begin
write('A[',i,']='); readln(a[i]);
end;
tc:=0;
tl:=0;
for i:=1 to n do
begin
if a[i] mod 2=0 then tc:=tc+a[i]
else tl:=tl+a[i];
end;
writeln('Tong cac so chan la: ',tc);
writeln('Tong cac so le la: ',tl);
t:=0;
for i:=1 to n do
t:=t+a[i];
writeln('Tong cua day so la: ',t);
readln;
end.
uses crt;
var a:array[1..1000]of integer;
i,n,dem1,dem2,t1,dem3,j,kt:integer;
begin
clrscr;
readln(n);
for i:=1 to n do
readln(a[i]);
dem1:=0;
dem2:=0;
t1:=0;
for i:=1 to n do
begin
if a[i] mod 2=0 then
begin
dem1:=dem1+1;
t1:=t1+a[i];
end;
else dem2:=dem2+1;
end;
dem3:=0;
for i:=1 to n do
if (a[i]>1) then
begin
kt:=0;
for j:=2 to trunc(sqrt(a[i])) do
if a[i] mod j=0 then kt:=1;
if kt=0 then dem3:=dem3+1;
end;
writeln(dem1, ' ',dem2);
writeln(t1);
writeln(dem3);
readln;
end.
Cho số nguyên dương N và dãy A gồm N số nguyên A1, A2, An và một số nguyên k. a) Đếm số lượng các số bằng 0 trong dãy A b) Đếm số lượng các số chẵn trong dãy A c) Tính tổng các số là bội của k trong dãy A d) Đếm số lượng các số bằng k trong dãy A
#include <bits/stdc++.h>
using namespace std;
long long x,i,n,nn;
int main()
{
cin>>n;
int dem=0;
for (i=1; i<=n; i++)
{
cin>>x;
if (x==0) dem++;
}
cout<<dem;
return 0;
}
Input:N nguyên dương ,dãy A gồm N số nguyên A1,A2,...An, số nguyên k
Output:- số lg các số bằng 0 trong dãy
- slg các số chẵn trong dãy
Tổng các số là bội của k trong dãy
- số lg các số các số bằng k tong dãy
+minh hoạ: N=8
....
cho n nguyên dương và dãy A gồm N số nguyên a1,a2...an. a, đếm số lượng số lẽ trong dãy .b tính tổng các số chia hết cho 3 và 4. c đưa ra vị trí của các số chẵn hỏi out là gì?
a: #include <bits/stdc++.h>
using namespace std;
long long n,i,x,dem;
int main()
{
cin>>n;
dem=0;
for (i=1; i<=n; i++)
{
cin>>x;
if (x%2!=0) dem++;
}
cout<<dem;
return 0;
}
Viết chương trình tính tổng các số chẵn trong dãy số nguyên A1, A2, ..., An. Biết rằng n, A1, A2, ..., An là các số nguyên nhập từ bàn phím (n>0)?
Các bạn giúp mình nha ,mình cần gấp lắm
program Tinhtong;
uses crt;
var a:array[1..100] of integer;
sum,n:integer;
begin clrscr;
write('Nhap n = '); readln(n);
for i:=1 to n do
begin
write('a[',i,'] = '); readln(a[i]);
end;
sum:=0;
for i:=1 to n do
if (a[i] mod 2 = 0) then sum:=sum+a[i];
write('Tong cac so chan = ',sum);
readln;
end.
uses crt;
var a:array[1..100]of integer;
i,n,dem,dem1,dem2,t:integer;
s:real;
begin
clrscr;
write('Nhap n='); readln(n);
for i:=1 to n do
begin
repeat
write('A[',i,']='); readln(a[i]);
until a[i]>0;
end;
for i:=1 to n do
write(a[i]:4);
writeln;
dem:=0;
for i:=1 to n do
if a[i]>10 then inc(dem);
writeln('So phan tu lon hon 10 la: ',dem);
dem1:=0;
dem2:=0;
for i:=1 to n do
begin
if a[i] mod 2=0 then inc(dem1)
else inc(dem2);
end;
writeln('So luong so chan la: ',dem1);
writeln('So luong so le la: ',dem2);
t:=0;
s:=1;
for i:=1 to n do
begin
if (i mod 2=0) and (a[i] mod 2<>1) then t:=t+a[i];
if (i mod 2=1) and (a[i] mod 2=0) then s:=s*a[i];
end;
writeln('Tong cac so o vi tri chan co gia tri le la: ',t);
writeln('Tich cac so o vi tri le co gia tri chan la: ',s:4:2);
writeln('Cac so le la: ');
for i:=1 to n do
if a[i] mod 2<>0 then write(a[i]:4);
writeln;
writeln('Cac so chan va lon hon 10 la: ');
for i:=1 to n do
if (a[i] mod 2=0) and (a[i]>10) then write(a[i]:4);
readln;
end.
giúp mình với ạ
Viết thuật toán tính tổng các số nguyên âm chẵn trong dãy a1, a2,...an
#include<iostream>
using namespace std;
int main() {
int g = 0;
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n; i++) {
g += a[i];
}
cout << g;
return 0;
}
Cho số nguyên dương N ( N ≤ 250 ) và dãy n số nguyên dương A1 A2,..., An mỗi số đều không vượt quá 500. Yêu cầu:
1) Đếm số lượng các phần tử có giá trị lẻ trong dãy số A.
2) Tính tổng giá trị các phần tử đứng vị trí chẵn trong dãy số A
3) Tính tổng giá trị các phần tử đứng vị trí lẻ trong dãy số A.
4) Tính tổng giá trị các phần tử là số chẵn đứng ở vị trí chẵn trong dãy A.
5) Tính tổng giá trị các phần tử là số lẻ đứng ở vị trí lẻ trong dãy A.
6) Tính trung bình cộng các phần tử trong dãy.
uses crt;
var a:array[1..250]of integer;
i,n,dem,t,t1,t2,t3,t4:integer;
begin
clrscr;
repeat
write('Nhap n='); readln(n);
until (0<n) and (n<=250);
for i:=1 to n do
begin
repeat
write('A[',i,']='); readln(a[i]);
until (0<a[i]) and (a[i]<=500);
end;
dem:=0;
for i:=1 to n do
if a[i] mod 2=1 then inc(dem);
writeln('So phan tu co gia tri le la: ',dem);
t:=0;
for i:=1 to n do
if i mod 2=0 then t:=t+a[i];
writeln('Tong cac phan tu co chi so chan la: ',t);
t1:=0;
for i:=1 to n do
if i mod 2=1 then t1:=t1+a[i];
writeln('Tong cac phan tu co chi so le la: ',t1);
t2:=0;
for i:=1 to n do
if (i mod 2=0) and (a[i] mod 2=0) then t2:=t2+a[i];
writeln('Tong cac phan tu chan co chi so chan la: ',t2);
t3:=0;
for i:=1 to n do
if (i mod 2=1) and (a[i] mod 2=1) then t3:=t3+a[i];
writeln('Tong cac phan tu co chi so le la: ',t3);
t4:=0;
for i:=1 to n do
t4:=t4+a[i];
writeln('Trung binh cong cac so trong day la: ',t4/n:4:2);
readln;
end.
: Viết chương trình nhập vào một dãy số A gồm N số nguyên a1, a2,…,an (n<=300, |ai| <= 40000).
a. Tính tổng dãy số A.
b. Tính tổng các số chia hết cho 3 có trong dãy số trên.
c. Đếm số lượng số chẵn, lẻ có trong dãy số trên.
f. tìm phần tử lớn nhất, nhỏ nhất của dãy số.
g. Tính tổng bình phương các số âm có trong dãy số trên.
Bài 1: Cho n và dãy số nguyên a1, a2, …, an. Tính và in ra màn hình tổng các số trong dãy a.
Bài 2. Cho n và dãy số nguyên a1, a2, …, an. In ra màn hình giá trị nhỏ nhất trong dãy a.
1:
#include <bits/stdc++.h>
using namespace std;
long long n,i,x,t;
int main()
{
cin>>n;
t=0;
for (i=1; i<=n; i++)
{
cin>>x;
t=t+x;
}
cout<<t;
return 0;
}