Viết chương trình nhập vào mảng gồm n số tự nhiên bất kì từ bàn phím tính và in ra màn hình tổng của các số là ước của 60 Bước 1 nhập mảng Bước 2 tong
Viết chương trình:
- Nhập mảng gồm n phần tử, với n được nhập vào từ bàn phím
- In mảng lên màn hình
- In ra màn hình số phần tử chia hết cho 2
- Tính và in ra màn hình tổng các phần tử của mảng
#include <bits/stdc++.h>
using namespace std;
long long a[10000],n,i,t;
int main()
{
cin>>n;
t=0;
for (i=1; i<=n; i++)
{
cin>>a[i];
t=t+a[i];
}
for (i=1; i<=n; i++) cout<<a[i]<<" ";
cout<<endl;
dem=0;
for (i=1; i<=n; i++)
if (a[i] %2==0) dem++;
cout<<dem<<endl;
cout<<t;
return 0;
}
viết chương trình nhập vào 1 mảng gồm n phần từ bàn phím . tính tổng các phần tử trong mảng .in kết quả ra màn hình.
-khai báo biến biến mảng .
-nhập n số phần tử mảng ,
-nhập mảng ,
-in mảng.
-tính tổng ,
-in kết quả ra màn hình
#include <bits/stdc++.h>
using namespace std;
long long a[1000],i,n,t;
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++) t+=a[i];
cout<<t;
return 0;
}
Viết chương trình nhập vào 1 mảng 1 chiều gồm n phần tử (với n được nhập vào từ bàn phím). Hãy thực hiện các công việc sau:
- Nhập các phần tử mảng từ bàn phím.
- Hiển thị mảng vừa nhập ra màn hình.
- In ra màn hình các số chính phương trong mảng.
- Đếm trong mảng có bao nhiêu phần tử có giá trị bằng 5. Nếu không có phần tử nào có giá trị bằng 5 thì in ra màn hình thông báo “Không có phần tử nào thỏa mãn điều kiện”
#include <bits/stdc++.h>
using namespace std;
long long n,i,a[10000];
int main()
{
cin>>n;
for (i=1; i<=n; i++)
cin>>a[i];
for (i=1; i<=n; i++) cout<<a[i]<<" ";
return 0;
}
Viết chương trình nhập vào từ bàn phím mảng B gồm N số nguyên (N<=200)và 1 số nguyên dương k:
1. Tính tổng các phần tử lẻ trong mảng A và đưa kết quả ra màn hình?
2. Tính tổng các phần tử là bội của K trong mảng A và đưa kết quả ra màn hình?
uses crt;
var a:array[1..200]of integer;
n,i,k,t,t1:integer;
begin
clrscr;
write('Nhap n='); readln(n);
for i:=1 to n do
begin
write('A[',i,']='); readln(a[i]);
end;
write('Nhap k='); readln(k);
t:=0;
for i:=1 to n do
if a[i] mod 2<>0 then t:=t+a[i];
t1:=0;
for i:=1 to n do
if a[i] mod k=0 then t1:=t1+a[i];
writeln('Tong cac phan tu le la: ',t);
writeln('Tong cac phan tu la boi cua ',k,' la: ',t1);
readln;
end.
Câu 1: Viết chương trình nhập n phần tử mảng 1 chiều là các số nguyên từ bàn phím, ghi mảng vừa nhập vào tệp MANG1CHIEU.TXT. Tìm giá trị nhỏ nhất trong mảng và đưa kết quả ra màn hình
Câu 2: Viết chương trình nhập 1 xâu bất kỳ từ bàn phím, In ra xâu sau khi đã loại bỏ các kí tự là chữ cái và ghi vào tệp CHUSO.TXT
giúp em với ạ . em cảm ơn nhiều...
cau 1:
uses crt;
var a:array[1..100] of integer;
n,i,min: integer;
begin
readln(n);
for i:=1 to n do
readln(a[i]);
min:=a[1];
for i:=2 to n do
if min>a[i] then min=a[i];
writeln(a[i]);
readln;
end.
cau 2:
uses crt;
g:text;
s:string;
const fo='CHUSO.TXT'
begin
assign(g,fo);
rewrite(g);
readln(s);
for i:=1 to length(s) do
if not((s[i] in ['a'..'z'])and(s[i] in ['A'..'Z])) then delete(s,i,1);
writeln(g,s);
end.
bài 1:Viết chương trình nhập vào một mảng số nguyên gồm n phần tử(n<100) nhập từ bàn phím thực hiện các công việc sau:
a)In ra màn hình các phần tử của mảng vừa nhập.
b) Sắp xếp dãy số nguyên theo thứ tự tăng dần.
c) Nhập số nguyên x bất kì.Tính số chắn lớn nhất không được vượt quá x.
Bài 1:
uses crt;
var a:array[1..100]of integer;
i,n,kt,max,x,j,tam:integer;
begin
clrscr;
write('Nhap n='); readln(n);
for i:=1 to n do
begin
write('A[',i,']='); readln(a[i]);
end;
writeln('Mang ban vua nhap la: ');
for i:=1 to n do
write(a[i]:4);
writeln;
for i:=1 to n-1 do
for j:=i+1 to n do
if a[i]>a[j] then
begin
tam:=a[i];
a[i]:=a[j];
a[j]:=tam;
end;
writeln('Day tang dan la: ');
for i:=1 to n do
write(a[i]:4);
writeln;
write('Nhap x='); readln(x);
max:=0;
kt:=0;
for i:=1 to n do
if (a[i] mod 2=0) and (a[i]<=x) then
begin
if max<a[i] then max:=a[i];
kt:=1;
end;
if kt=0 then writeln('Trong day khong co so le')
else writeln('So chan lon nhat khong vuot qua ',x,' la: ',max);
readln;
end.
Viết chương trình nhập mảng 1 chiều gồm N số nguyên (N được nhập từ bàn phím <=250). Tính và in ra màn hình Tổng của cá phần tử chia hết cho 3 có trong mảng vừa nhập.
bprogram min
uses crt
var A: array [1...250] of integer
tong :integer;
begin
tong:=0
writeln(‘nhap n:’); readln (n)
forr i:=1 to n do
begin
writeln (‘nhap phan tu thu ‘,i);
readln (A[i] );
end
for i:=1 to n do
begin
if (A[i] mod 3=0)
then tong:=tong +A[i];
end
writeln (Tong cac phan tu chia het cho 3)
readln ;
end
Program Vidu;
Uses crt;
Type Mang1C = array[1..250] of Integer;
Var A:Mang1C;
i:integer;
Begin
clrscr;
Writeln
For i:=1 to 250 do
Begin
Write
Readln
End;
Write
For i:=1 to 250 do
Write
Readln;
End.
a) Viết chương tình nhập vào từ bàn phím mảng A gồm n số nguyên.In ra màn hình số nguyên lớn nhất
b) Viết chương trình nhập vào từ bàn phím mảng B gồm n số nguyên.In ra màn hình số nguyên bé nhất
a)
uses crt;
var a:array[1..100]of integer;
i,n,max:integer;
begin
clrscr;
write('Nhap n='); readln(n);
for i:=1 to n do
begin
write('A[',i,']='); readln(a[i]);
end;
max:=a[1];
for i:=1 to n do
if max<a[i] then max:=a[i];
writeln(max);
readln;
end.
b)
uses crt;
var a:array[1..100]of integer;
i,n,min:integer;
begin
clrscr;
write('Nhap n='); readln(n);
for i:=1 to n do
begin
write('A[',i,']='); readln(a[i]);
end;
min:=a[1];
for i:=1 to n do
if min>a[i] then min:=a[i];
writeln(min);
readln;
end.
Viết chương trình nhập vào một mảng nguyên, gồm n phần tử nhập từ bàn phím, in mảng đã nhập ra màn hình, tính trung bình cộng của mảng đó.
uses crt;
var a:array[1..100]of integer;
i,n,t:integer;
begin
clrscr;
readln(n);
for i:=1 to n do
begin
readln(a[i]);
end;
for i:=1 to n do
write(a[i]:4);
readln;
t:=0;
for i:=1 to n do
t:=t+a[i];
writeln(t/n:4:2);
readln;
end.