Viết chương trình nhập vào 1 mảng a gồm 10 phần tử
a. In ra mảng và nhập
b. In ra các phần tử lẻ
c. In ra các phần tử chẵn
d. In ra các phần tử có giá trị bằng 5
e. In ra các phần tử chia hết cho 3
Giúp em với ạ
- Viết chương trình nhập 1 mảng số nguyên gồm n phần tử
- In ra tất cả phần tử của mảng vừa nhập
- Đếm xem có bao nhiêu phần tử có giá trị là ước của 10.
- In ra các phần tử ở vị trí chẵn.
- In ra các phần tử ở vị trí lẻ.
Program HOC24;
var d,i,n: integer;
a: array[1..32000] of integer;
begin
write('Nhap N: '); readln(n);
for i:=1 to n do
begin
write('a[',i,']='); readln(a[i]);
end;
write('Cac phan tu cua mang vua nhap la: ');
for i:=1 to n do write(a[i],' ');
writeln;
d:=0;
for i:=1 to n do if 10 mod a[i]=0 then d:=d+1;
writeln('Co ',d,' phan tu co gia tri la uoc cua 10');
write('Cac phan tu o vi tri chan la: ');
for i:=1 to n do if i mod 2=0 then write(a[i],' ');
writeln;
write('Cac phan tu o vi tri le la: ');
for i:=1 to n do if i mod 2=1 then write(a[i],' ');
readln
end.
Viết chương trình nhập 1 mảng số nguyên gồm n phần tử
- in ra các phần tử của mảng vừa nhập
- In ra các phần tử có giá trị chẵn
- Đếm xem có tất cả bao nhiêu phần tử chẵn. In ra tổng số phần tử chẵn
uses crt;
var n,i,chan,tonchan:integer;
a:array[1..100] of integer;
begin
write('nhap n '); readln(n);
for i:=1 to n do
begin
write('a[i]='); readln(a[i]);
end;
writeln("in cac phan tu trong mang");
for i:=1 to n do
begin
writeln(a[i]);
end;
writeln("in cac phan tu chan");
chan:=0;
for i:=1 to n do
if n mod 2=0 then begin writeln(a[i]); chan:=chan+1; end;
write('tong cac phan tu chan ',chan);
readln;
end.
BT1: Viết chương trình nhập vào một mảng gồm 10 phần tử số nguyên. In ra màn hình mảng vừa nhập và tính tổng các phần tử trong mảng. BT2: Viết chương trình nhập vào một mảng gồm 10 phần tử số thực. In ra màn hình các phần tử có chỉ số chẵn trong mảng. BT3: Viết chương trình nhập vào một mảng gồm n phần tử số nguyên. In ra màn hình các phần tử có giá trị chẵn trong mảng. BT4: Viết chương trình nhập vào một mảng gồm n phần tử số nguyên. In ra màn hình phần tử có giá trị lớn nhất trong mảng. BT5: Viết chương trình nhập vào một mảng gồm n phần tử số thực. In ra màn hình chỉ số của phần tử nhỏ nhất trong mảng. BT6: Viết chương trình nhập vào một mảng gồm n phần tử số nguyên. Đếm xem trong mảng vừa nhập có bao nhiêu phần tử lẻ.
6:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n,A[100],i,dem=0;
cin>>n;
for (int i=1; i<=n; i++) cin>>A[i];
for (int i=1;i<=n; i++)
if (A[i]%2!=0) dem++;
cout<<dem;
return 0;
}
5:
#include <bits/stdc++.h>
using namespace std;
int main()
{
long long n,nn=1e6,A[1000];
cin>>n;
for (int i=1; i<=n; i++) cin>>A[i];
for (int i=1; i<=n; i++)
nn=min(nn,A[i]);
for (int i=1; i<=n; i++)
if (nn==A[i]) cout<<i<<" ";
return 0;
}
Viết chương trình nhập vào mảng a gồm n phần tử thuộc kiểu số nguyên, yêu cầu: - In ra màn hình các phần tử trong mảng - In ra màn hình số lượng các phần tử là số chẵn hoặc số lẻ.
#include <bits/stdc++.h>
using namespace std;
long long a[1000],n,i,dem1,dem2;
int main()
{
cin>>n;
for (i=1; i<=n; i++) cin>>a[i];
for (i=1; i<=n; i++) cout<<a[i]<<" ";
cout<<endl;
dem1=0;
dem2=0;
for (i=1; i<=n; i++)
{
if (a[i]%2==0) dem1++;
else dem2++;
}
cout<<dem1<<" "<<dem2;
return 0;
}
Cho mảng A gồm N phần tử nguyên a1,a2,..,an. Với N < 103 nhập từ bàn phím. a. Hãy viết chương trình nhập mảng A. b. In ra màn hình tất cả các phần tử chẵn có trong mảng A. c. In ra màn hình tất cả các phần tử lẻ có trong mảng A.
Uses crt;
var i,n: longint;
a: array[1..103] of longint;
begin clrscr;
readln(n);
for i:=1 to n do read(a[i]); readln;
for i:=1 to n do if(a[i] mod 2=0) then write(a[i],' ');
for i:=1 to n do if(a[i] mod 2<>0) then write(a[i],' ');
readln;
end.
Bài 1: Viết chương trình nhập mảng số nguyên A có n phần tử. Tính tổng các phần tử nguyên âm lẻ và in tổng đó ra màn hình. Bài 2: Viết chương trình nhập mảng số nguyên A có n phần tử. Tính tích các phần tử nguyên dương chẵn và in tích đó ra màn hình.
Bài 1:
#include <bits/stdc++.h>
using namespace std;
long long x,n,i,t;
int main()
{
cin>>n;
t=0;
for (i=1; i<=n; i++)
{
cin>>x;
if ((x<0) and (x%2!=0)) t=t+x;
}
cout<<t;
return 0;
}
viết chương trình nhập vào mảng một chiều.gồm phần tử số nguyên N được nhập từ bàn phím. N nguyên, dương, N<=10
A, in mảng vừa nhập ra màn hình
B, tính tổng các phần tử có giá trị chẵn, in kết qua ra màn hình
C, đếm số lượng các phần tử chia hết cho 3
D, tính trung bình cộng các số có giá trị lẻ
E, in ra màn hình các phần tử là số nguyên tố
mong được giúp đỡ
#include <bits/stdc++.h>
using namespace std;
long long a[100],n,i,t,t1,dem,dem1;
//chuongtrinhcon
bool ktnt(long long x)
{
if (x<=1) return(false);
for (int i=2; i*i<=x; i++)
if (x%i==0) return(false);
return true;
}
//chuongtrinhchinh
int main()
{
cin>>n;
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++) if (a[i]%2==0) t=t+a[i];
cout<<t<<endl;
dem=0;
for (i=1; i<=n; i++) if (a[i]%3==0) dem++;
cout<<dem<<" ";
t1=0;
dem1=0;
for (i=1; i<=n; i++)
if (a[i]%2!=0)
{
t1+=a[i];
dem1++;
}
cout<<fixed<<setprecision(2)<<(t1*1.0)/(dem1*1.0)<<endl;
for (i=1; i<=n; i++)
if (ktnt(a[i])==true) cout<<a[i]<<" ";
return 0;
}
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 nhập vào 1 mảng gồm có n phần tử
a) in mảng đó ra màn hình
b) tính tổng các phần tử trong mảng
c)tính tổng các phần tử chẵn/lẽ
d) tính tổng các phần tử chia hết cho 2