1. Viết chương trình nhập vào một số nguyên dương n và in ra màn hình dãy số tự nhiên từ 1 tới n. 2. Viết chương trình nhập vào một số nguyên n và in ra màn hình các số nguyên chẵn trong khoảng từ 1 tới n.
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;
}
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 nhập vào một dãy số nguyên n phần tử, n nhập từ bàn phím.
a) In ra màn hình dãy số vừa nhập xếp theo thứ tự tăng dần.
b) In ra màn hình các số nguyên tố trong dãy số vừa nhập và đếm xem đã nhập vào bao nhiêu số nguyên tố.
1. Viết chương trình nhập vào một số nguyên dương n. Hãy in ra số nguyên tố nhỏ nhất lớn hơn n. 2. Viết chương trình nhập vào một xâu. In ra màn hình số lớn nhất trong xâu đó.
câu 1
Program Nguyen_to;
Var n,i:integer;
Function NT(n:integer):Boolean;
Var ok: Boolean;
i: integer;
Begin ok:=true;
for i:=2 to n-1 do if (n mod i)= 0 then ok:=ok and false;
if n < 2 then NT:=false else NT:=ok;
End;
Begin Write('Nhap n: ');
Readln(n); i:=n;
Repeat i:=i+1;
Until NT(i);
Write('So nguyen to nho nhat lon hon ',n, 'la: ',i);
Readln End.
câu 2
uses crt;
const so: set of char=['0','1','2','3','4','5','6','7','8','9'];
var a:array[1..100] of integer;
st,b:string;
c,l,i,n,j:integer;
s, Max: integer;
begin clrscr;
write('Nhap xau:');
readln(st);
l:=length(st);
i:=1;
n:=0;
repeat if (st[i] in so) then begin b:=''
repeat b:=b+st[i];
inc(i);
until (not(st[i] in so)) or (i>l);
inc(n);
val(b,a[n],c);
end;
inc(i);
until i>l;
Max:=a[1];
for i:=2 to n do If Max<A[i] Then Max:=A[i];
Writeln('Phan tu lon nhat cua mang:', Max);
readln;
end.
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.
Viết chương trình nhập dãy số nguyên A gồm N phần tử (N được nhập vào từ bàn phím (N<=150).
a. In dãy số ra màn hình, mỗi phần tử cách nhau một ký tự trống.
b. Tính tổng các phần tử chẵn và in kết quả ra màn hình.
Viết chương trình nhập dãy số nguyên A gồm N phần tử (N được nhập vào từ bàn phím (N<=150).
a. In dãy số ra màn hình, mỗi phần tử cách nhau một ký tự trống.
b. Tính tổng các phần tử chẵn và in kết quả ra màn hình.
program BaiTap;
var
A: array[1..150] of integer;
N, i, sum: integer;
begin
write('Nhap so phan tu cua day A (N <= 150): ');
readln(N);
for i := 1 to N do
begin
write('Nhap phan tu thu ', i, ': ');
readln(A[i]);
end;
writeln('Day so vua nhap la:');
for i := 1 to N do
write(A[i], ' ');
writeln;
sum := 0;
for i := 1 to N do
if A[i] mod 2 = 0 then
sum := sum + A[i];
writeln('Tong cac phan tu chan trong day la: ', sum);
readln;
end.
2.viết chương trình nhập vào số nguyên dương n. A. Tìm và in ra màn hình các số từ 1 đến n chia hết cho 2. .B. tìm và in ra màn hình số từ 1-n ko chia hết cho 2
Program HOC24;
var i,n: integer;
begin
write('Nhap N: '); readln(n);
write('Cac so tu 1 den ',n,' chia het cho 2 la: ');
for i:=1 to n do if i mod 2=0 then write(i,' ');
writeln;
for i:=1 to n do if i mod 2=1 then write(i,' ');
readln
end.