Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Odd even number sum

Tags:

pascal

I need to calculate odd/even sum, here is what I've got so far:

PROGRAM EvenOddSum;
USES
  WinCrt;
VAR
  odd, even, x: INTEGER;

  BEGIN

  WriteLn('Calculation of sum');
  WriteLn;
  odd := 0;
  even := 0;
  Write('Enter value(s)');
  WHILE x > 0 DO BEGIN
   IF x mod 2:= 0 THEN BEGIN
     even := even + x;
   ELSE
     odd := odd + x;

  ReadLn(x);
  END;
  WriteLn;
  WriteLn('Even sum is = ', even);
  WriteLn('Odd sum is =', odd);
  END.

I use freepascal.org compiler and I get this error :

SYNTAX error THEN expected but := found

And I just can't see the problem with this code.

like image 501
London Avatar asked Feb 21 '26 04:02

London


2 Answers

In Pascal, := is the assignment operator. Replace it with = on the line that reads IF x mod 2:= 0 THEN BEGIN.

Also, remove the BEGIN. The result should read:

IF x mod 2 = 0 THEN
like image 170
moteutsch Avatar answered Feb 27 '26 08:02

moteutsch


It's in here:

IF x mod 2:= 0 THEN BEGIN

The := is used for assignment, use '=' or '==' for comparison. (Off the top of my head, I don't know if Pascal uses '=', '==', or both for comparisons. One of them should do the trick).

like image 23
S.L. Barth Avatar answered Feb 27 '26 09:02

S.L. Barth