Bài 1:
*Dùng For..do
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.
*Dùng While..do
uses crt;
var n,i:integer;
begin
clrscr;
write('Nhap n='); readln(n);
i:=1;
while i<=n do
begin
if n mod i=0 then write(i:4);
inc(i);
end;
readln;
end.
Bài 2:
*Dùng For..do
uses crt;
var n,i:integer;
begin
clrscr;
write('Nhap n='); readln(n);
for i:=1 to n do
if (n mod i=0) and (i mod 2=0) then write(i:4);
readln;
end.
*Dùng While..do
uses crt;
var n,i:integer;
begin
clrscr;
write('Nhap n='); readln(n);
i:=1;
while i<=n do
begin
if (n mod i=0) and (i mod 2=0) then write(i:4);
inc(i);
end;
readln;
end.
Bài 3:
*Dùng For..do
uses crt;
var n,i,t:integer;
begin
clrscr;
write('Nhap n='); readln(n);
t:=0;
for i:=1 to n do
if n mod i=0 then t:=t+i;
writeln('Tong cac uoc cua ',n,' la: ',t);
readln;
end.
*Dùng While..do
uses crt;
var n,i,t:integer;
begin
clrscr;
write('Nhap n='); readln(n);
t:=0;
i:=1;
while i<=n do
begin
if n mod i=0 then t:=t+i;
inc(i);
end;
writeln('Tong cac uoc cua ',n,' la: ',t);
readln;
end.
Bài 4:
*Dùng For..do
uses crt;
var i,dem:integer;
begin
clrscr;
dem:=0;
for i:=100 to 999 do
if i mod 3=0 then inc(dem);
writeln('So cac so co 3 chu so chia het cho 3 la: ',dem);
readln;
end.
*Dùng While..do
uses crt;
var i,dem:integer;
begin
clrscr;
dem:=0;
i:=100;
while i<=999 do
begin
if i mod 3=0 then inc(dem);
inc(i);
end;
writeln('So cac so co 3 chu so chia het cho 3 la: ',dem);
readln;
end.
Bài 5:
*Dùng For..do
uses crt;
var i,t,n:integer;
begin
clrscr;
write('Nhap n='); readln(n);
t:=0;
for i:=1 to n do
if i mod 2=0 then t:=t+i;
writeln('Tong cac so chan tu 1 toi ',n,' la: ',t);
readln;
end.
*Dùng While..do
uses crt;
var i,t,n:integer;
begin
clrscr;
write('Nhap n='); readln(n);
t:=0;
i:=1;
while i<=n do
begin
if i mod 2=0 then t:=t+i;
inc(i);
end;
writeln('Tong cac so chan tu 1 toi ',n,' la: ',t);
readln;
end.
Bài 6:
*Dùng For..do
uses crt;
var i,t,n:integer;
begin
clrscr;
write('Nhap n='); readln(n);
t:=0;
for i:=1 to n do
if i mod 2=1 then t:=t+i;
writeln('Tong cac so le tu 1 toi ',n,' la: ',t);
readln;
end.
*Dùng While..do
uses crt;
var i,t,n:integer;
begin
clrscr;
write('Nhap n='); readln(n);
t:=0;
i:=1;
while i<=n do
begin
if i mod 2=1 then t:=t+i;
inc(i);
end;
writeln('Tong cac so le tu 1 toi ',n,' la: ',t);
readln;
end.
Bài 7:
*Dùng For..do
uses crt;
var i,n:integer;
begin
clrscr;
write('Nhap n='); readln(n);
for i:=1 to n do
if i mod 5=0 then write(i:4);
readln;
end.
*Dùng While..do
uses crt;
var i,n:integer;
begin
clrscr;
write('Nhap n='); readln(n);
i:=1;
while i<=n do
begin
if i mod 5=0 then write(i:4);
inc(i);
end;
readln;
end.
Bài 8:
*Dùng For..do
uses crt;
var i,n,dem:integer;
begin
clrscr;
write('Nhap n='); readln(n);
dem:=0;
for i:=1 to n do
if (n mod i=0) and (i mod 2=1) then inc(dem);
writeln('So uoc le cua ',n,' la: ',dem);
readln;
end.
*Dùng While..do
uses crt;
var i,n,dem:integer;
begin
clrscr;
write('Nhap n='); readln(n);
dem:=0;
i:=1;
while i<=n do
begin
if (n mod i=0) and (i mod 2=1) then inc(dem);
i:=i+1;
end;
writeln('So uoc le cua ',n,' la: ',dem);
readln;
end.
Bài 9:
*Dùng For..do
uses crt;
var i,n:integer;
begin
clrscr;
write('Nhap n='); readln(n);
writeln('Bang cuu chuong ',n,' la: ');
for i:=1 to 10 do
writeln(n*i);
readln;
end.
*Dùng While..do
uses crt;
var i,n:integer;
begin
clrscr;
write('Nhap n='); readln(n);
writeln('Bang cuu chuong ',n,' la: ');
i:=1;
while i<=10 do
begin
writeln(n*i);
inc(i);
end;
readln;
end.
Bài 10:
*Dùng For..do
uses crt;
var i,n,kt:integer;
begin
clrscr;
write('Nhap n='); readln(n);
kt:=0;
for i:=2 to n-1 do
if n mod i=0 then kt:=1;
if kt=0 then write(n,' la so nguyen to')
else write(n,' la hop so');
readln;
end.
*Dùng While..do
uses crt;
var i,n:integer;
begin
clrscr;
write('Nhap n='); readln(n);
i:=2;
while (n mod i<>0) and (i<=n) do
begin
inc(i);
end;
if i=n then writeln(n,' la so nguyen to')
else writeln(n,' la hop so');
readln;
end.