Cant obtain a correct answer it sums incorectly and multiplicates too, i must use repeat when solving this problem, i suppose the problem is in sum:=i+i
but i dont know how to solve it
program SAD;
uses crt;
var a, i, sum, prod: integer;
begin
clrscr;
sum:=0;
prod:=0;
{Sum}
repeat
for i:=1 to 26 do
if i mod 4 = 0 then sum:=i+i;
until i = 26;
{Multiplication}
repeat
for a:=1 to 26 do
if a mod 4 = 0 then prod:=a*a;
until a = 26;
writeln('Suma numerelor divizate la 4 este:', sum);
writeln('Produsul numerelor divizate la 4 este:', prod);
end.
I think the instruction "use repeat" probably intends that you should avoid using for
as well.
There are a few errors in your code:
In the sum loop, you should add i
to sum
, not to itself.
In the prod loop, since you set prod
to zero at the start, it will stay as zero because zero times anything is zero. So you need to adjust the logic of your prod
calculation so that if prod
is zero, when the mod 4
condition is satisfied, you set prod
to the current value of a
, otherwise you multiply it by a
.
Here is some code which fixes the above points and avoids the use of for
.
program Sad;
uses crt;
var
a, i, sum, prod: integer;
begin
clrscr;
sum:=0;
prod:=0;
{Sum}
i := 0;
repeat
inc(i);
if (i mod 4) = 0 then
sum := sum + i;
until i = 26;
{Multiplication}
a :=0;
repeat
inc(a);
if a mod 4 = 0 then begin
if prod = 0 then
prod := a
else
prod := prod * a;
end;
until a = 26;
writeln('Suma numerelor divizate la 4 este:', sum);
writeln('Produsul numerelor divizate la 4 este:', prod);
readln;
end.