Viết chương trình nhập vào số nguyên n sau đó tính tổng và tích từ 1 đến n bằng hàm while
a, Viết chương trình nhập vào số nguyên dương, tính N!=1*2*3*...*N
VD N=5 thì in ra 5!=120
b, Viết chương trình tính tổng số chẵn từ 1 đến N với N là số nguyên sau đó hiển thị ra tông
VD N=10 thì tổng số chẵn là 30
c, Viết chương trình nhập vào của một số nguyên dương N, kiểm tra xem có phải số nguyên tố
a)
uses crt;
VAR
n, d, i: integer;
BEGIN
clrscr;
Writeln ('Nhap vao n='); readln (n);
d : = 1;
For i: = 1 to n do
d: = d*i;
Writeln ('d=',d);
Readln;
END.
c)
uses crt;
VAR
n, i, demuoc: integer;
BEGIN
clrscr;
Writeln ('Nhap vao n='); readln (n);
demuoc: = 0;
For i: = 1 to n do
If n mod i = 0 then
demuoc : = demuoc + 1;
If demuoc = 2 then
Writeln ('n la so nguyen to')
ELSE
Writeln ('n khong phai la so nguyen to');
Readln ;
END.
Còn phần b bạn tự nghĩ nha!
Chúc bạn học tốt!
Bài tập 1: Viết chương trình nhập vào n và k, tính và in ra màn hình giá trị C = Yêu cầu viết hàm Def GT(x): ….. Để tính x! (x! = 1*2*3*…*x) Bài tập 2: Viết chương trình nhập vào n nguyên dương, tính và in ra: ☠ Tổng các số chẵn từ 1 à n ☠ Tổng các số lẻ từ 1 à n ☠ Tổng các số chia hết cho k (k nguyên dương nhập vào từ bàn phím) từ 1 à n Yêu cầu viết hàm con cho mỗi việc tính tổng ở trên. Bài tập 3 Viết chương trình nhập vào số nguyên n, tính và in ra tổng các số tạo thành số đó. Bài tập 4 Cho mảng A = [1, 2, 3, 4, 5] Viết chương trình nhập vào k, tính và in ra: (mỗi yêu cầu là một hàm) Tổng các phần tử chẵn trong mảng A Tổng các phần tử lẻ trong mảng A. Tổng các phần tử nguyên tố trong mảng A
Câu 1: Viết chương trình nhập vào một số nguyên n. In ra màn hình tích các số từ 1 đến n.
Câu 2: Viết chương trình nhập vào một số nguyên n. In ra màn hình tích các số chẵn từ 1 đến n.
Câu 3: Viết chương trình nhập vào một số nguyên n. In ra màn hình tích các số lẻ từ 1 đến n.
c1:
#include <bits/stdc++.h>
using namespace std;
long long i,n,s;
int main()
{
cin>>n;
s=1;
for (i=1; i<=n; i++) s=s*i;
cout<<s;
return 0;
}
Câu 2:
#include <bits/stdc++.h>
using namespace std;
long long i,n,s;
int main()
{
cin>>n;
s=1;
for (i=1; i<=n; i++) if (i%2==0) s=s*i;
cout<<s;
return 0;
}
(Viết chương trình Pascal sử dụng câu lệnh while...do)
1. Viết chương trình nhập vào 1 dãy gồm n số nguyên và tính tổng, trung bình của dãy vừa nhập.
2. Viết chương trình nhập 1 số nguyên x, kiểm tra số vừa nhập có phải số nguyên tố hay không.
Bài 1
Var s,i:integer;
tb:real;
Begin
Write('Nhap n = ');readln(n);
i:=1;
s:=0;
While i<=n do
Begin
s:=s+i;
i:=i+1;
End;
tb:=s/n;
Writeln('Tong la ',s);
Write('Trung binh la ',tb:10:2);
Readln;
End.
Bài 2
Var i,n,souoc:integer;
Begin
Write('Nhap n = ');readln(n);
i:=1;
While i <= n do
Begin
i:=i + 1;
If n mod i = 0 then souoc:=souoc + 1;
End;
If souoc = 1 then write(n,' la so nguyen to')
Else write(n,' khong la so nguyen to');
Readln;
End.
Hãy viết chương trình tính tổng tích, của các số tự nhiên từ 1 đến N (n được nhập từ bàn phím) bằng cách dùng câu lệnh For...do hoặc While...do?
program TinhTongTich;
var
N, i, Tong, Tich:integer;
begin
writeln('Nhap so nguyen duong N:');
readln(N);
i:=1;
Tong:=0;
Tich:=1;
while i<=N do
begin
Tong:=Tong+i;
Tich:=Tich*i;
i:=i+1;
end;
writeln('Tong cac so tu 1 den ', N, ' la: ', Tong);
writeln('Tich cac so tu 1 den ', N, ' la: ', Tich);
readln;
end.
1. Viết chương trình nhập vào 1 số tự nhiên P. Tính và in ra màn hình các số lẻ và tổng các số lẻ từ 1 đến P. 2.Viết chương trình nhập vào 1 số tự nhiên N. Tính và in ra màn hình các số chẵn và tổng các số chẵn từ 1 đến N. 3. Viết chương trình nhập vào 1 số tự nhiên N. Tính tổng các số là ước của N. 4. Viết chương trình nhập vào 1 số tự nhiên N. Tính tổng các số là bội của 3 mà nhỏ hơn hoặc bằng N.
Câu 1:
Program HOC24;
var i,p: integer;
t: longint;
begin
write('Nhap P: '); readln(p);
t:=0;
for i:=1 to p do if i mod 2<>0 then t:=t+i;
write('Tong cac so le la: ',t);
readln
end.
Câu 2:
Program HOC24;
var i,n: integer;
t: longint;
begin
write('Nhap N: '); readln(n);
t:=0;
for i:=1 to n do if i mod 2=0 then t:=t+i;
write('Tong cac so chan la: ',t);
readln
end.
Câu 3:
Program HOC24;
var i,t,n: integer;
begin
write('Nhap N: '); readln(n);
t:=0;
for i:=1 to n do if n mod i=0 then t:=t+i;
write('Tong cac uoc cua ',n,' la: ',t);
readln
end.
Viết chương trình tính tổng các số chẵn từ 1 đến n với n nhập vào từ bàn phím.
In ra các số chia hết cho 2 đó trên một hàng ngang. Dùng vòng lặp while
n = int(input('Nhập số n: '))
sum = 0
i = 1
while i <= n:
if i % 2 == 0:
sum += i
print(i, end=' ')
i += 1
print()
print('Tổng các số chẵn từ 1 đến n là:', sum)
n = int(input('Nhập số n: '))
sum = 0
i = 1
while i <= n:
if i % 2 == 0:
sum += i
print(i, end=' ')
i += 1
print()
print('Tổng các số chẵn từ 1 đến n là:', sum)
Viết chương trình nhập vào 1 dãy số nguyên gồm n phần tử (với n được nhập từ bàn phím, n<=15) Tính tổng các số của dãy và in kết quả tổng tính đc ra màn hình(dùng câu lệnh lặp while..do)
Var a:array[1..15] of integer;
i,s:integer;
Begin
I:=1;
While i<=15 do
Begin
Write('Nhap phan tu thu ',i,' = ');readln(a[i]);
s:=s+a[i];
i:=i+1;
End;
Write('Tong la ',s);
Readln;
End.
1.viết chương trình nhập vào mảng A gồm N phần tử (N<= 500) từ bàn phím. Sau đó, tính tổng các phần tử âm chia hết cho cho 7
2.viết chương trình nhập vào mảng C gồm 10 phần tử từ bàn phím. Sau đó xuất ra các phần tử là số nguyên tố
3.viết chương trình tạo nhập vào mảng D gồm K phần tử (K<= 200) từ bàn phím. Sau đó cho biết có bao nhiêu phần tử chẵn có 2 chữ số
mn giúp em giải với ạ
Câu 1:
uses crt;
var a:array[1..500]of integer;
i,n,t:integer;
begin
clrscr;
write('Nhap n='); readln(n);
for i:=1 to n do
begin
write('A[',i,']='); readln(a[i]);
end;
t:=0;
for i:=1 to n do
if (a[i]<0) and (a[i] mod 7=0) then t:=t+a[i];
writeln('Tong cac so am chia het cho 7 la: ',t);
readln;
end.
Câu 2:
uses crt;
var c,a:array[1..10]of integer;
i,kt,j,dem:integer;
begin
clrscr;
for i:=1 to 10 do
begin
write('C[',i,']='); readln(c[i]);
end;
dem:=0;
for i:=1 to 10 do
if c[i]>1 then
begin
kt:=0;
for j:=2 to c[i]-1 do
if c[i] mod j=0 then kt:=1;
if kt=0 then
begin
inc(dem);
a[dem]:=c[i];
end;
end;
if dem=0 then writeln('Trong day khong co so nguyen to')
else begin
writeln('Cac so nguyen to trong day la: ');
for i:=1 to dem do
write(a[i]:4);
end;
readln;
end.
Câu 3:
uses crt;
var d:array[1..200]of integer;
i,k,dem:integer;
begin
clrscr;
write('Nhap k='); readln(k);
for i:=1 to k do
begin
write('D[',i,']='); readln(d[i]);
end;
dem:=0;
for i:=1 to k do
if (a[i] mod 2=0) and (a[i]>=10) then inc(dem);
writeln('So phan tu chan co 2 chu so la: ',dem);
readln;
end.
Bài 1: Viết chương trình pascal thực hiện các yêu cầu sau: a) Nhập từ bàn phím 1 số nguyên n b) In ra màn hình kết quả tổng các số nguyên từ 1 đến n (sử dụng câu lệnh lặp while...do) Bài 2: Viết chương trình pascal thực hiện câc yêu cầu sau: a) Nhập từ bàn phím 1 số nguyên dương n b) In ra màn hình kết quả tổng các số lẻ từ 1 đến n (sử dụng câu lệnh lặp while...do giúp mình với mình đang cần gấp rất gấp luôn
Bài 1:
uses crt;
var n,i,s:integer;
begin
clrscr;
write('Nhap n='); readln(n);
s:=0;
i:=1;
while i<=n do
begin
s:=s+i;
inc(i);
end;
writeln('Tong cac so trong khoang tu 1 den ',n,' la: ',s);
readln;
end.
Bài 2:
uses crt;
var n,i,s:integer;
begin
clrscr;
write('Nhap n='); readln(n);
s:=0;
i:=1;
while i<=n do
begin
s:=s+i;
i:=i+2;
end;
writeln('Tong cac so le trong khoang tu 1 den ',n,' la: ',s);
readln;
end.