👤

Se introduce un numar natural n. Sa se scrie un program care calculeaza suma sirului cu n elemente: 2/1 + 2/3 + 4/3 + 4/5 + 6/5 + 6/7... In pascal


Răspuns :

program suma;

var n, i, a:integer;

   s:real;

begin

  write('n= '); read(n);

  s:=0;

  for i:=1 to (n div 2) do

    begin

      a:=2*i;

      s:=s+a/(a-1) + a/(a+1);

    end;

 if (n mod 2=1) then s:=s+(a+2)/(a+1);

 writeln('Suma solicitata = ', s);

end.

**************************************************

program sumaVarianta2;

var n, i, a, ind:integer;

   s:real;

begin

  write('n= '); read(n);

  s:=0; ind:=0; a:=0;

  for i:=1 to n do

    begin

       ind:=ind+1;

       if ind mod 2=1 then a:=a+2;

       if i mod 2=1 then s:=s+a/(a-1)

       else s:=s + a/(a+1);

    end;

 writeln('Suma solicitata = ', s);

end.