Viết chương trình nhập vào số nguyên dương N. Đưa ra màn hình số nguyên tố gần N nhất!
nhanh nha!
PASCAL
0 bình luận về “Viết chương trình nhập vào số nguyên dương N. Đưa ra màn hình số nguyên tố gần N nhất! nhanh nha! PASCAL”
program oken; uses crt; var n,i,j,s1,s2,dem: longint; begin clrscr; write(‘Nhap n: ‘); readln(n); for i:=1 to n do begin dem:=0; for j:=2 to (i div 2) do if (i mod j=0) then dem:=dem+1; if dem=0 then s1:=i; end; for i:=(n*10) downto n do begin dem:=0; for j:=2 to (i div 2) do if (i mod j=0) then dem:=dem+1; if dem=0 then s2:=i; end; if s1=s2 then writeln(‘So nguyen to gan ‘,n,’ nhat: ‘,s1) else if (abs(s1-n))>(s2-n) then writeln(‘So nguyen to gan ‘,n,’ nhat: ‘,s2) else if (abs(s1-n))<(s2-n) then writeln(‘So nguyen to gan ‘,n,’ nhat: ‘,s1) else writeln(‘So nguyen to gan ‘,n,’ nhat: ‘,s1,’ va ‘,s2); readln; end.
uses crt; var i,j,n:longint; function nt(a:longint):boolean; var i:longint; begin i:=2; while (a>1)and(a mod i<>0) do inc(i); nt:=i=a; end; begin clrscr; write(‘n=’);readln(n); i:=n; j:=n; while(i>2)and(not(nt(i)))do dec(i); while(not(nt(j)))do inc(j); write(‘So nguyen to gan ‘,n,’ nhat la: ‘); if abs(i-n)<=abs(n-j) then write(i) else write(j); if (abs(i-n)=abs(n-j))and(i<j) then write(‘,’,j); readln; end.
program oken;
uses crt;
var n,i,j,s1,s2,dem: longint;
begin
clrscr;
write(‘Nhap n: ‘); readln(n);
for i:=1 to n do
begin
dem:=0;
for j:=2 to (i div 2) do
if (i mod j=0) then
dem:=dem+1;
if dem=0 then
s1:=i;
end;
for i:=(n*10) downto n do
begin
dem:=0;
for j:=2 to (i div 2) do
if (i mod j=0) then
dem:=dem+1;
if dem=0 then
s2:=i;
end;
if s1=s2 then
writeln(‘So nguyen to gan ‘,n,’ nhat: ‘,s1)
else if (abs(s1-n))>(s2-n) then
writeln(‘So nguyen to gan ‘,n,’ nhat: ‘,s2)
else if (abs(s1-n))<(s2-n) then
writeln(‘So nguyen to gan ‘,n,’ nhat: ‘,s1)
else
writeln(‘So nguyen to gan ‘,n,’ nhat: ‘,s1,’ va ‘,s2);
readln;
end.
uses crt;
var i,j,n:longint;
function nt(a:longint):boolean;
var i:longint;
begin
i:=2;
while (a>1)and(a mod i<>0) do inc(i);
nt:=i=a;
end;
begin
clrscr;
write(‘n=’);readln(n); i:=n; j:=n;
while(i>2)and(not(nt(i)))do dec(i);
while(not(nt(j)))do inc(j);
write(‘So nguyen to gan ‘,n,’ nhat la: ‘);
if abs(i-n)<=abs(n-j) then write(i) else write(j);
if (abs(i-n)=abs(n-j))and(i<j) then write(‘,’,j);
readln;
end.
//////////////////////////////////////////
Test: