viết chương trình pascal in ra các số là ước dương của một số dương n (n nhập vào từ bàn phím)
Viết Chương Trình Pascal Cho Nhập N Nguyên , Dương Từ Bàn Phím . In ra màn hình : -Các Ước Của N Không Kể N -Xác Định N Có Phải Là Số Hoàn Hảo -Tất Cả Số Hoàn Hảo Trong Phạm Vi Từ 1-> N
program bai_toan;
var
N, i, sum: integer;
begin
write('Nhap so N: ');
readln(N);
write('Cac uoc cua ', N, ' khong ke ', N, ' la: ');
for i := 1 to N - 1 do
if N mod i = 0 then
write(i, ' ');
writeln;
sum := 0;
for i := 1 to N - 1 do
begin
if N mod i = 0 then sum := sum + i;
end;
if sum = N then writeln(N, ' la so hoan hao')
else writeln(N, ' khong phai la so hoan hao');
writeln;
writeln('Tat ca so hoan hao trong pham vi 1 -> ', N, ' la:');
for i := 1 to N do
begin
sum := 0;
for j := 1 to i - 1 do
begin
if i mod j = 0 then sum := sum + j;
end;
if sum = i then writeln(i);
end;
readln;
end.
1. Thực hành
Bài 1: Viết chương trình nhập số nguyên n từ bàn phím. Kiểm tra và in ra màn hình số n có hợp lệ hay không với điều kiện n là số nguyên dương chẵn.
Bài 2: Viết chương trình Pascal nhập các số nguyên x và y từ bàn phím, in giá trị của x và y ra màn hình. Sau đó hoán đổi các giá trị của x và y rồi in lại ra màn hình giá trị của x và y.
• Ví dụ cho x:=5; y:=7.Hoán đổi để x:=7; y:=5.Cho 1 biến trung gian là z, ta hoán đổi x, y cho nhau bằng cách:
• z:=y { gán giá trị số 7 vào biến nhớ z }
• y:=x { gán giá trị số 5 vào biến nhớ y }
• x:=z { gán giá trị số 7 vào biến nhớ x }
Bài 3: Viết chương trình nhập vào ba số nguyên a, b và c từ bàn phím. Tính và in ra màn hình:
X= a + b + c
Y= (a + b)2 / c.
Chạy lại chương trình và nhập giá trị cho a là 10.5, quan sát kết quả và nêu nhận xét.
Bài 2:
#include <bits/stdc++.h>
using namespace std;
long long x,y;
int main()
{
cin >>x>>y;
cout<<x<<" "<<y;
swap(x,y);
cout<<x<<" "<<y;
return 0;
}
a, Viết chương trình pascal nhập vào một dãy số nguyên n từ bàn phím b, in dãy vừa nhập ra màn hình. c, đếm xem bao nhiêu số dương. d, in kết quả số dương ra màn hình.
Program HOC24;
var a: array[1..32000] of integer;
i,n,d: integer;
begin
write('Nhap N: ');
for i:=1 to n do
begin
write('A[',i,']='); readln(a[i]);
end;
write('Day vua nhap la: ');
for i:=1 to n do write(a[i],' '); writeln;
d:=0;
for i:=1 to n do if a[i]>0 then d:=d+1;
write('Co ',d,' so duong');
readln
end.
Bài 1: Viết chương trình Pascal nhập số từ bàn phím. Xuất ra màn hình:
+ Tính n!
+ Nhập số x từ bàn phím.Tính nx
Bài 2: Viết chương trình Pascal nhập số n từ bàn phím. Xuất ra màn hình
+ Dòng 1: Các ước của n
+ Dòng 2: Nếu n là số lẻ thì xuất ra màn hình các số nguyên tố từ 2 đến n
+ Dòng 3: Nếu n là chẵn thì xuất ra màn hình các số hoàn hảo từ 1 đến n
+ Dòng 4: Tổng các ước của n
( 2 bài khác nhau mọi người làm riêng ra giúp mình ạaaaaaa)
Viết chương trình nhập từ bàn phím một số nguyên dương N (1<= N < 1000) in ra màn hình các thông tin sau :
a) Số các ước số nguyên dương của số N
b) Tổng các ước số nguyên dương của N
(m.n giúp em với)
#include <bits/stdc++.h>
using namespace std;
long n,i,dem,t;
int main()
{
cin>>n;
dem=0;
t=0;
for (i=1; i<=n;i++)
if (n%i==0)
{
dem++;
t=t+i;
}
cout<<dem<<" "<<t;
return 0;
}
Viết chương trình nhập vào từ bàn phím một số nguyên dương n, in ra màn hình các số lẻ nhỏ hơn hoặc bằng số nguyên dương n. Chương trình yêu cầu nhập lại nếu n <=0. (Sử dụng câu lệnh While … do)
program Le_Nho_Hon_Hoac_Bang_n;
uses crt;
var
n, i: integer;
begin
clrscr;
write('Nhap vao mot so nguyen duong n: ');
readln(n);
while n <= 0 do
begin
writeln('So ban nhap khong hop le. Xin vui long nhap lai: ');
readln(n);
end;
clrscr;
writeln('Cac so le nho hon hoac bang ', n, ' la:');
i := 1;
while i <= n do
begin
if i mod 2 <> 0 then
writeln(i);
i := i + 1;
end;
readln;
end.
Hãy viết chương nhập vào 1 dãy N số nguyên từ bàn phím ( N là Nguyên dương được nhập từ bàn phím vào N>10) . Tính và in ra màn hình trung bình của các số
program trungbinh;
uses crt;
var n,i: integer;
s:real;
A: array[1..1000] of integer;
begin
clrscr;
write('Nhap do dai day: '); readln(n);
writeln('Nhap gia tri cua day: ');
S:=0;
for i:=1 to n do
begin
write('A[',i,']= ');
readln(A[i]);
S:=S+A[i];
end;
S:=S/n;
write('Trung binh cua cac so cua day do la: ',S);
readln;
end.
Viết chương trình tìm tất cả các ước của số nguyên dương n (n nhập từ bàn phím).
program Tim_Uoc;
uses crt;
var
n, i: integer;
begin
clrscr;
write('Nhap vao mot so nguyen duong n: ');
readln(n);
writeln('Cac uoc cua so ', n, ' la: ');
for i:=1 to n do
begin
if n mod i = 0 then
writeln(i);
end;
readln;
end.
Viết chương trình tìm ước nguyên tố của một số nguyên dương n nhập từ bàn phím
Program HOC24;
var i,n: integer;
function nt(x: longint): boolean;
var j: longint;
begin
nt:=true;
if (x=2) or (x=3) then exit;
nt:=false;
if (x=1) or (x mod 2=0) or (x mod 3=0) then exit;
j:=5;
while j<=trunc(sqrt(x)) do
begin
if (x mod j=0) or (x mod (j+2)=0) then exit;
j:=j+6;
end;
nt:=true;
end;
begin
write('Nhap N: '); readln(n);
write('Cac uoc nguyen to cua ',n,' la: ');
for i:=1 to n do if (n mod i=0) and nt(i) then write(i,' ');
readln
end.
viết đoạn chương trình in các ước của số nguyên dương n ra màn hình với n được nhập từ bàn phím. (gợi ý: cho vòng lặp for có biến đếm i chạy từ 1-n, kiểm tra xem biến i có là ước ước số của n khi n chia hết cho i) *
for i:=1 to n do
if n mod i=0 then write(i:4);
Chương trình đầy đủ:
uses crt;
var n,i:integer;
begin
clrscr;
write('Nhap n='); readln(n);
for i:=1 to n do
if n mod i=0 then write(i:4);
readln;
end.