Trang chủ Tin Học Lớp 7 24. Cho số tự nhiên N. Viết chương trình in...

24. Cho số tự nhiên N. Viết chương trình in ra ước số nguyên tố lớn nhất của N. Vi dụ: n=10, ước số nguyên tố lớn nhất là 5 Vì 10 có các ước số là: 1 5 10,

Câu hỏi :

Giúp ạ! Đề (Hình bên dưới) Cảm ơn mn!

image

Lời giải 1 :

BÀI LÀM

Câu 24:

Uses CRT;
Var i,n,j: Integer;
Function ktnt (n:Integer): Boolean;
Var d,j: Integer;
Begin
  If n<2 then
   ktnt:=false
  Else
   Begin
    d:=0;
    For i:=1 to n do
     If n mod i=0 then
      d:=d+1;
    If d=2 then
     ktnt:=true
    Else
     ktnt:=false;
   End;
End;
Begin
  ClrScr;
  Write ( 'Nhap so nguyen n: ' );
  Readln (n);
  i:=1;
  While i<=n do
   Begin
   If (n mod i=0) and (ktnt(i)=true) then
    j:=i;
    i:=i+1;
   End;
  Write(j);
  Readln;
End.

Câu 25:

Uses CRT;
Var i,n,j: Integer;
Begin
  ClrScr;
  Write ( 'Nhap so nguyen n: ' );
  Readln (n);
  i:=n div 2;
  While (i<=n) and (n mod i=0) do
    Begin
     j:=i;
     i:=i-1;
    End;
  Write ( 'Uoc so thuc lon nhat cua n la ',j);
  Readln;
End.

Câu 26:

Uses CRT;
Var i,n,j: Integer;
Function ktnt (n: Integer): Boolean;
Var d: Integer;
Begin
  If n<2 then
   ktnt:=false
  Else
   Begin
    d:=0;
    For i:=1 to n do
     If n mod i=0 then
      d:=d+1;
    If d=2 then
     ktnt:=true
    Else
     ktnt:=false;
   End;
End;
Begin
  ClrScr;
  Write ( 'Nhap so nguyen n: ');
  Readln (n);
  i:=1;
  While i<n do
    Begin
     If ktnt(i)=true then
      j:=i;
     i:=i+1;
    End;
  Write ( 'So nguyen to lon nhat be hon n la: ' ,j);
  Readln;
End.

Câu 27:

Uses CRT;
Var i,n,j: Integer;
Function ktnt(n:Integer): Boolean;
Var d:Integer;
Begin
  If n<2 then
   ktnt:=false
  Else
   d:=0;
   For i:=1 to n do
    If n mod i=0 then
     d:=d+1;
   If d=2 then
    ktnt:=true
   Else
    ktnt:=false;
End;
Begin
  ClrScr;
  Write ( 'Nhap so nguyen n: ');
  Readln (n);
  i:=n+1;
  While i>n do
    Begin
     If ktnt(i)=true then
      Begin
       j:=i;
       i:=0;
      End;
     i:=i+1;
    End;
  Write ( 'So nguyen to be nhat lon hon n la ' ,j);
  Readln;
End.

@TrangCute

#Beautiful

→CHÚC BẠN HỌC TỐT!←

Thảo luận

-- very good
-- ghê bây
-- J đấy coan kia
-- Đừng nói lên đây lấy đáp án yêu dấu của t đấy
-- có j đâu
-- buff he m :vv
-- buff j má
-- =))))

Lời giải 2 :

24

uses crt;
var i,n:longint;
function ktnt(n:longint):boolean;
var i:longint;
begin
    if n < 2 then exit(false);
    if n < 4 then exit(true);
    if n mod 2 = 0 then exit(false);
    if n mod 3 = 0 then exit(false);
    i:=5;
    while i*i <= n do
        begin
            if n mod i = 0 then exit(false);
            if n mod (i+2) = 0 then exit(false);
            inc(i,6);
        end;
    exit(true);
end;
begin
    clrscr;
    readln(n);
    i:=n;
    while i <= n do
        begin
            if n mod i = 0 then
                if ktnt(i) then
                    begin
                        write(i);
                        readln;
                        exit;
                    end;
            dec(i);
        end;
readln;
end.

25

uses crt;
var i,n:longint;
begin
    clrscr;
    readln(n);
    i:=n div 2;
    while i >= 1 do
        begin
            if n mod i = 0 then
                begin
                    write(i);
                    readln;
                    exit;
                end;
            dec(i);
        end;
readln;
end.

26

uses crt;
var i,n:longint;
function ktnt(n:longint):boolean;
var i:longint;
begin
    if n < 2 then exit(false);
    if n < 4 then exit(true);
    if n mod 2 = 0 then exit(false);
    if n mod 3 = 0 then exit(false);
    i:=5;
    while i*i <= n do
        begin
            if n mod i = 0 then exit(false);
            if n mod (i+2) = 0 then exit(false);
            inc(i,6);
        end;
    exit(true);
end;
begin
    clrscr;
    readln(n);
    while ktnt(n) = false do dec(n);
    writeln(n);
readln;
end.

27

uses crt;
var i,n:longint;
function ktnt(n:longint):boolean;
var i:longint;
begin
    if n < 2 then exit(false);
    if n < 4 then exit(true);
    if n mod 2 = 0 then exit(false);
    if n mod 3 = 0 then exit(false);
    i:=5;
    while i*i <= n do
        begin
            if n mod i = 0 then exit(false);
            if n mod (i+2) = 0 then exit(false);
            inc(i,6);
        end;
    exit(true);
end;
begin
    clrscr;
    readln(n);
    inc(n);
    while ktnt(n) = false do inc(n);
    writeln(n);
readln;
end.

Bạn có biết?

Tin học, tiếng Anh: informatics, tiếng Pháp: informatique, là một ngành khoa học chuyên nghiên cứu quá trình tự động hóa việc tổ chức, lưu trữ, xử lý và truyền dẫn thông tin của một hệ thống máy tính cụ thể hoặc trừu tượng (ảo). Với cách hiểu hiện nay, tin học bao hàm tất cả các nghiên cứu và kỹ thuật có liên quan đến việc mô phỏng, biến đổi và tái tạo thông tin.

Nguồn : Wikipedia - Bách khoa toàn thư

Tâm sự 7

Lớp 7 - Năm thứ hai ở cấp trung học cơ sở, một cuồng quay mới lại đến vẫn bước tiếp trên đường đời học sinh. Học tập vẫn là nhiệm vụ chính!

Nguồn : ADMIN :))

Liên hệ hợp tác hoặc quảng cáo: gmail

Điều khoản dịch vụ

Copyright © 2021 HOCTAPSGK