Meekanovich
Meekanovich

Reputation: 7

I'm having some problems with even and odd numbers in Pascal

I'm having some problems with my program in Pascal. I need to create a program which will calculate even and odd sums of a number's decomposition. For example, if my number is 10 the program should write that sum of even numbers is 30 (since 2,4,6,8,10 are the even numbers) and it should write that sum of odd numbers is 25 (since 1,3,5,7,9 are odd numbers). Here is what i tried

program odd_even;
var 
  a,sumeven,sumodd,even,odd : integer;

begin
  writeln('Enter a number : ');
  readln(a);
  if a mod 2 = 0 then a=even;
  if a mod 2 not=0 then a=odd;
  for a:1 to a do begin
    sumeven:=0;
    sumeven:=sumeven+even
    writeln('Sum of even numbers is : ',sumeven);
    sumodd:=0;
    sumodd:=sumodd+odd;
    writeln('Sum of odd numbers is : ',sumodd),
  end;
  readln
end.

The compiler says that my if part is illegal but I don't understand how I can fix it, I also tried with else but it says the same thing. If someone could help me out I would be really thankful.

Upvotes: 0

Views: 612

Answers (1)

SiR
SiR

Reputation: 177

First of all, welcome to the world of programming! There are several errors in your code:

The initialization of your result variables

sumEven:=0;
sumOdd:=0;

should be before your for loop checking odd/even

if a mod 2 = 0 then a=even;
if a mod 2 not=0 then a=odd;

should be inside your loop and you should check not whether a (your input number) is odd/even but the value of your loop variable:

for i := 1  to a do

   begin
       if (i mod 2 <> 0) then sumOdd := sumOdd+1 else sumEven := sumEven+1 ;
   end;

Printing the results should be of course after your loop. Good luck!

Upvotes: 1

Related Questions