1. Đếm số phần tử không chia hết cho 3 trong mảng nguyên A ( A <= 100)
2. Tính tổng các phần tử không âm trong mảng nguyên A ( với A <= 250 , A{i}<= 500)
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
uses crt;
var a:array[1..100]of integer;
i,n,dem:integer;
begin
clrscr;
write('Nhap n='); readln(n);
for i:=1 to n do
begin
write('A[',i,']=');readln(a[i]);
end;
dem:=0;
for i:=1 to n do
if (a[i] mod 3=0) and (a[i] mod 5=0) then inc(dem);
writeln('So phan tu chia het cho 3 va 5 trong day la: ',dem);
readln;
end.
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 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;
}
const fi='dl.pas'
fo='qk.pas'
var f1,f2:text;
i,n,dem,t,dem1,dem2,kt,j:integer;
a:array[1..100]of integer;
begin
assign(f1,fi); reset(f1);
assign(f2,fo); rewrite(f2);
readln(f1,n);
for i:=1 to n do
read(f1,a[i]);
dem:=0;
for i:=1 to n do
if (a[i] mod 2<>0) and (a[i] mod 7=0) then inc(dem);
t:=0;
for i:=1 to n do
if (10<=a[i]) and (a[i]<=30) then t:=t+a[i];
dem1:=0;
for i:=1 to n do
if (a[i]<0) and (a[i] mod 2<>0) then inc(dem1);
dem2:=0;
for i:=1 to n do
if a[i]>1 then
begin
kt:=0;
for j:=2 to a[i]-1 do
if a[i] mod j=0 then kt:=1;
if kt=0 then inc(dem2);
end;
writeln(f2,'So phan tu le chia het cho 7 la: ',dem);
writeln(f2,'Tong cac phan tu thuoc [10,30] la: ',t);
writeln(f2,'So cac phan tu am le la: ',dem1);
writeln(f2,'So phan tu la so nguyen to la: ',dem2);
close(f1);
close(f2);
end.
Cho mảng A gồm 10 phần tử thuộc kiểu nguyên. Viết hàm đếm số lượng số nguyên chia hết cho 7 của mảng A. Mọi người giúp mình với ạ
type mang=array[1..10]of integer;
function demsochiahet(a:mang):integer;
var dem,i,n:integer;
begin
dem:=0;
for i:=1 to n do
if a[i] mod 7=0 then inc(dem);
demsochiahet:=dem;
end;
Viết chương trình nhập giá trị cho N phần tử của mảng số nguyên A1 A2 A3 ... AN và số nguyên K ; Đếm và tính tổng các phần tử của mảng A có giá trị chia hết cho K
Nhập vào mảng A (\(n\le200\)) là số nguyên. Viết chương trình C++ tính tổng các phần tử có giá trị chẵn và đếm các phần tử chia hết cho 3
#include <bits/stdc++.h>
using namespace std;
long long x,n,i,dem,t;
int main()
{
cin>>n;
dem=0;
t=0;
for (i=1; i<=n; i++)
{
cin>>x;
if (x%2==0) t+=x;
if (x%3==0) dem++;
}
cout<<"Tong cac so chan la: "<<t<<endl;
cout<<"So luong phan tu chia het cho 3 la: "<<dem;
return 0;
}
Bài 1: Hãy viết chương trình con thực hiện các công việc sau trên mảng một chiều:
+ Nhập vào 1 mảng
+ In mảng + Sắp xếp mảng tăng dần
+ Tính tổng các phần tử trong mảng
+ Đếm các phần tử chia hết cho 3 trong mảng
+ Đếm các số âm, dương trong mảng
Bài 2: Viết chương trình con để tính diện tích các hình sau:
- Diện tích hình tam giác theo công thức herrong khi biết độ dài 3 cạnh a, b, c của tam giác. S=sqrt(p*(p-a)*(p-b)*(p-c)) với p=(a+b+c)/2
- Diện tích hình thang khi biết độ dài chiều cao h và 2 cạnh đáy a, b S=(a+b)*h/2 - Diện tích hình vuông khi biết độ dài cạnh.
Bài 3: Viết chương trình giải phương trình bậc 2 sử dụng chương trình con
-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.