Visualizzazione dei risultati da 1 a 5 su 5
  1. #1

    [PASCAL] While..do & Repeat...until

    Stampare a video la somma dei quadratidei primi N (n dato da tastiera) numeri naturali.
    Dati due numeri N,M > 0 e N < M, stampare la somma dei numeri compresi tra N e M.
    Potete aiutarmi?

  2. #2
    Utente di HTML.it L'avatar di pdpmpd
    Registrato dal
    Jan 2003
    Messaggi
    448
    prima procedura (stampa i quadrati da 0 a numero), il numero da tastiera lo acquisisci dal prog. principale:
    codice:
    procedure stampa(numero: integer);
    var
     cx: integer;
    begin
      for cx:=1 to numero do
        writeln('quadrato di ',cx,' = ',cx*cx);
    end;
    seconda procedura
    codice:
    procedure stampadue(n,m:integer);
    var
    cx,ctr: integer;
    begin
      ctr:=0;
      for cx:=n to m do inc(ctr,cx);
      writeln('Somma: ',ctr);
    end;
    Drug misuse is not a disease, it is a decision, like the decision to step out in front of a moving car. [...] In this particular life-style the motto is "be happy now because tomorrow you are dying", but the dying begins almost at once, and the happiness is a memory.

  3. #3
    Utente di HTML.it L'avatar di pdpmpd
    Registrato dal
    Jan 2003
    Messaggi
    448
    ok, tu volevi il repeat..until (ma per queste funzioni non è affatto utile).

    codice:
    procedure stampa(numero: integer);
    var
     cx: integer;
    begin
      cx:=1  
      while cx<=numero do begin
        writeln('quadrato di ',cx,' = ',cx*cx);
        inc(cx);
      end;
    end;
    
    procedure stampadue(n,m:integer);
    var
    cx,ctr: integer;
    begin
      ctr:=0;
      cx:=n;
      repeat
        inc(ctr,cx);
        inc(cx);
      until cx=m;
      writeln('Somma: ',ctr);
    end;
    Drug misuse is not a disease, it is a decision, like the decision to step out in front of a moving car. [...] In this particular life-style the motto is "be happy now because tomorrow you are dying", but the dying begins almost at once, and the happiness is a memory.

  4. #4
    Moderatore di Programmazione L'avatar di LeleFT
    Registrato dal
    Jun 2003
    Messaggi
    17,304
    Attenzione: la prima chiedeva la somma dei quadrati dei primi n numeri, non di visualizzare tutti i quadrati dei primi n numeri:
    codice:
    procedure SommaQuadrati (n: integer);
    var somma: integer;
        i: integer;
    begin
       somma := 0;
    
       for i := 0 to n do
          somma := somma + n * n;
    
       writeln('La somma dei primi n quadrati è: ', somma);   
    end;
    
    Con while...do:
    procedure SommaQuadrati (n: integer);
    var somma: integer;
        i: integer;
    begin
       somma := 0;
       i := 0;
       while (i <= n) do
       begin
          somma := somma + n * n;
          i := i + 1;
       end;
    end;
    E la si potrebbe trasformare in una funzione:
    codice:
    function SommaQuadrati (n: integer) : integer;
    var somma: integer;
        i: integer;
    begin
       somma := 0;
       i := 0;
       while (i <= numero) do
       begin
          somma := somma + n * n;
          i := i + 1;
       end;
    
       SommaQuadrati := somma;
    end;
    
    oppure ricorsiva:
    
    function SommaQuadrati (n: integer) : integer;
    var risultato: integer;
    begin
       if (n = 0) Then
          risultato := 0
       else
          risultato := (n * n) + SommaQuadrati(n - 1);
    
       SommaQuadrati := risultato;
    end;
    Ciao.
    "Perchè spendere anche solo 5 dollari per un S.O., quando posso averne uno gratis e spendere quei 5 dollari per 5 bottiglie di birra?" [Jon "maddog" Hall]
    Fatti non foste a viver come bruti, ma per seguir virtute e canoscenza

  5. #5
    Utente di HTML.it L'avatar di pdpmpd
    Registrato dal
    Jan 2003
    Messaggi
    448
    oooopps! sorry
    Drug misuse is not a disease, it is a decision, like the decision to step out in front of a moving car. [...] In this particular life-style the motto is "be happy now because tomorrow you are dying", but the dying begins almost at once, and the happiness is a memory.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.