Dear Sir,

Could you tell me how can I do the following question?

A positive integer is entered from the keyboard. If it's even, all even integers from 2 up to and including the entered integer are displayed. If odd, all odd integers from 1 up to and including the entered integer are summed, and the sum displayed.

For example if the number entered was 20, the display would be:
2 4 6 8 10 12 14 16 18 20
Alternatively, if the number entered was11, the display should be:
36

Cheers,

program solution;
{A positive integer is entered from the keyboard.
so we need 3 vars
user is your choice,i is a loop var,
sum contains the sum of the odd numbers,so..}
var user,i,sum:integer;

begin
   sum:=0;
   write('give me a positive integer number: ');
   readln(user);
   if (odd(user)=true)then begin{if user is an odd number}
     for i:=1 to user do begin
         if i mod 2 = 1 then sum:=sum+i;
     end;
     write(sum);
   end
   else {if user is an even number then }
   begin
      for i:=1 to user do begin
          if i mod 2 = 0 then write(i,' ');
      end;
   end;
   readln;
end.
{
created by FlamingClaw 2009.08.21.
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.