Cant find the mistake.... - Programmers Heaven

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

#### Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

# Cant find the mistake....

Posts: 1Member
[code]program ocjena;
uses wincrt;
var
i, k, S, y, n:real;
begin
S:=0;
i:=1;
y:=i+1;
K:=1/i -1/y;
write ('Unesi krajnji broj: ');
for i:=1 to n do
for i:=1 to 10 do
begin
S:=S+K;
i:=i+1;
if (i=n) then
S:=S+1/i ;
if (y=n) then
S:=S-1/y;
end;
writeln ('S iznosi', S:2:2);
end.[/code]

This is my program which calculate something like that...or should be..:)
1/1 - 1/2 + 1/3 - 1/4 + 1/5 - 1/6 + 1/7 - 1/8... +- 1/n...
But i cant find out why it sais that for loop (only one) is not good....

• Posts: 268Member
: [code]: program ocjena;
: uses wincrt;
: var
: i, k, S, y, n:real;
: begin
: S:=0;
: i:=1;
: y:=i+1;
: K:=1/i -1/y;
: write ('Unesi krajnji broj: ');
: for i:=1 to n do [color=Red]<----------[/color]
: for i:=1 to 10 do [color=Red]<--- Same variable to control 2 loops[/color]
: begin
: S:=S+K;
: i:=i+1;
: if (i=n) then
: S:=S+1/i ;
: if (y=n) then
: S:=S-1/y;
: end;
: writeln ('S iznosi', S:2:2);
: end.[/code]:
:
: This is my program which calculate something like that...or should
: be..:)
: 1/1 - 1/2 + 1/3 - 1/4 + 1/5 - 1/6 + 1/7 - 1/8... +- 1/n...
: But i cant find out why it sais that for loop (only one) is not
: good....
:

[code][color=Blue]
program ocjena;

var i,n:word;
s:real;

begin
write('Unesi krajnji broj: ');readln(n); {<-- Dunno what it means}
s:=0;
if n>0 then
if n<2 then s:=1 else begin
s:=1;
for i:=2 to n do
if odd(i) then s:=s+1/i
else s:=s-1/i;
end;
writeln('S iznosi: ', s:2:6);
end.
[/color][/code]
• Posts: 268Member
More simplified

[code][color=Blue]
program ocjena;

var i,n:word;
s:real;

begin