Cho mảng A gồm n phần tử là các số nguyên a, Nhập vào mảng A từ bàn phím b, tính tổng của các phần tử chẵn c, tính tổng của các phần tử lẻ d, đếm xem có bao nhiêu phần tử trong dãy chia hết cho 3
Cho mảng A gồm 5 phần tử thuộc kiểu nguyên.Hãy lập chương trình: a. Nhập mảng và in mảng ra màn hình b. Tính tổng các phần tử của mảng A c. In ra màn hình các phần tử ở vị trí lẻ của mảng
#include <bits/stdc++.h>
using namespace std;
long long a[5],i,n,t;
int main()
{
n=5;
for (i=1; i<=n; i++)
cin>>a[i];
for (i=1; i<=n; i++) cout<<a[i]<<" ";
cout<<endl;
t=0;
for (i=1; i<=n; i++) t+=a[i];
cout<<t<<endl;
for (i=1; i<=n; i++)
if (i%2!=0) cout<<a[i]<<" ";
return 0;
}
-Viết chương trình:
a)Nhập vào mảng 1 chiều N phần tử nguyên(N<=100)
b)In mảng vừa nhập.
c)Tính tổng các phần tử lẻ/chẵn trong mảng
d)Đếm xem trong mảng có bao nhiều phần tử chia hết cho 3.
e)Tính tổng các phần tử trong mảng.
f)Sắp xếp các mảng theo thứ tự tăng dần.
Viết chương trình vào 1 mảng số nguyên phải thực hiện các công việc: a). Đếm xem có bao nhiêu phần tử dương và phần tử âm trong mảng b). Tính và đưa ra màng hình tổng các số dương và tổng câc số âm
#include <bits/stdc++.h>
using namespace std;
long long x,n,i,dem1,dem2,t1,t2;
int main()
{
cin>>n;
dem1=0;
dem2=0;
t1=0;
t2=0;
for (i=1; i<=n; i++)
{
cin>>x;
if (x>0)
{
dem1++;
t1+=x;
}
if (x<0)
{
dem2++;
t2+=x;
}
}
cout<<dem1<<" "<<dem2<<endl;
cout<<t1<<" "<<t2;
return 0;
}
cho mảng A gồm N phần tử thuộc kiểu nguyên (0<N<103). Viết chương trình đếm số lượng số lẻ và chia hết cho 3 của mảng A
Cho mảng một chiều gồm n phần tử thuộc kiểu số nguyên viết chương trình nhâp mảng xuất dữ liệu của mảng đếm và in ra các phần tử chính phương trong mảng sắp xếp các phần tử mảng theo thứ tự số chẵn tăng số lẻ giảm
1. Viết chương trình nhập vào mảng gồm n số nguyên, - Đếm và đưa ra số phần tử chẵn của mảng - đưa ra số chẵn lớn nhất trong mảng - Tính tổng các số ở vị trí lẻ trong mảng - Tìm giá trị phần tử lẻ nhỏ nhất
uses crt;
var a:array[1..100]of integer;
i,n,dem,max,t,min,dem1:integer;
begin
clrscr;
write('n='); readln(n);
for i:=1 to n do
begin
write('A[',i,']='); readln(a[i]);
end;
dem:=0;
max:=-32000;
for i:=1 to n do
begin
if a[i] mod 2=0 then
begin
dem:=dem+1;
if max<a[i] then max:=a[i];
end;
if dem=0 then writeln('Trong day khong co so chan')
else begin
writeln('So so chan la: ',dem);
writeln('So chan lon nhat la: ',max);
end;
t:=0;
for i:=1 to n do
if i mod 2=1 then t:=t+a[i];
writeln('Tong cac so o vi tri le la: ',t);
min:=maxint;
dem1:=0;
for i:=1 to n do
if a[i] mod 2<>0 then
begin
inc(dem1);
if min>a[i] then min:=a[i];
end;
if dem1=0 then writeln('Trong day khong co so le')
else writeln('So le nho nhat la: ',min);
readln;
end.
Viết chương trình nhập vào mảng số nguyên gồm n phần tử và in tổng các giá trị phần tử là số chẳn, Các phần tử là số lẻ, các phần tử chia hết cho 2 và 3.
uses crt;
var a:array[1..100]of integer;
i,n,t1,t2,t3:integer;
begin
clrscr;
readln(n);
for i:=1 to n do readln(a[i]);
t1:=0;
t2:=0;
t3:=0;
for i:=1 to n do
begin
if a[i] mod 2=0 then t1:=t1+a[i]
else t2:=t2+a[i];
if (a[i] mod 2=0) and (a[i] mod 3=0) then t3:=t3+a[i];
end;
writeln(t1);
writeln(t2);
writeln(t3);
readln;
end.
Cho mảng 1 chiều gồm 20 phần tử số nguyên. Hãy viết ct a) Đếm số phần tử chia cho 9 dư 2 b) Tính tổng các phần tử chia hết cho 5 c) Tính tích các phần tử chia hết cho 7
#include <bits/stdc++.h>
using namespace std;
long long a[20],n=20,i,dem,t,s;
int main()
{
for (i=1; i<=n; i++) cin>>a[i];
dem=0;
for (i=1; i<=n; i++) if (a[i]%9==2) dem++;
t=0;
for (i=1; i<=n; i++) if (a[i]%5==0) t+=a[i];
s=1;
for (i=1; i<=n; i++)
if (a[i]%7==0) s*=a[i];
cout<<dem<<endl;
cout<<t<<endl;
cout<<s;
return 0;
}