Stampare a video la somma dei quadratidei primi N (n dato da tastiera) numeri naturali.Potete aiutarmi?Dati due numeri N,M > 0 e N < M, stampare la somma dei numeri compresi tra N e M.
Stampare a video la somma dei quadratidei primi N (n dato da tastiera) numeri naturali.Potete aiutarmi?Dati due numeri N,M > 0 e N < M, stampare la somma dei numeri compresi tra N e M.
prima procedura (stampa i quadrati da 0 a numero), il numero da tastiera lo acquisisci dal prog. principale:
seconda proceduracodice:procedure stampa(numero: integer); var cx: integer; begin for cx:=1 to numero do writeln('quadrato di ',cx,' = ',cx*cx); end;
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.
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.
Attenzione: la prima chiedeva la somma dei quadrati dei primi n numeri, non di visualizzare tutti i quadrati dei primi n numeri:
E la si potrebbe trasformare in una funzione: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;
Ciao.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;
"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
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.