0

Dear Sir,

Please help me on Delphi. Could you tell me how can I do the following question?

Calculate and display the sum of the even numbers from 2 to 20 inclusive.

Cheers,

5
Contributors
7
Replies
8
Views
7 Years
Discussion Span
Last Post by EdFallon
Featured Replies
  • 1
    sknake 1,622   7 Years Ago

    [code=delphi] procedure TForm1.Button1Click(Sender: TObject); Var i1, iResult: Integer; begin iResult := 0; for i1 := 2 to 20 do begin if ((i1 mod 2) = 0) then Inc(iResult, i1); end; ShowMessage(IntToStr(iResult)); end; [/code] Read More

  • Hi Turbomen :D If you want to create a console application then this is also a solution [code=delphi] Program EvenOrNot; {$APPTYPE CONSOLE} Uses SysUtils; Var i,counter:LongInt; TheOdds:Boolean; {true or false} Begin i:=2; counter:=0; Write('The even numbers are: '); While i<=21 Do Begin TheOdds:=Odd(i); If (TheOdds=False) Then Begin Write(i,' '); counter:=counter+i;{increase … Read More

  • Hi Turbomen :D We store the sum of the even numbers in the variable named *Counter*. When the program is running first *Counter* 's value is zero. Counter:=0; *TheOdds* variable is holds a *true* or *false* value changed by the built in function Odd(i) that wait longint and send back … Read More

1
procedure TForm1.Button1Click(Sender: TObject);
Var
  i1, iResult: Integer;
begin
  iResult := 0;
  for i1 := 2 to 20 do
  begin
    if ((i1 mod 2) = 0) then Inc(iResult, i1);
  end;
  ShowMessage(IntToStr(iResult));
end;
Comments
his code is works as excepted
1

Hi Turbomen :D
If you want to create a console application then this is also a solution

Program EvenOrNot;

{$APPTYPE CONSOLE}

Uses
  SysUtils;

Var i,counter:LongInt;
    TheOdds:Boolean; {true or false}
Begin
   i:=2;
   counter:=0;
   Write('The even numbers are: ');
   While i<=21 Do Begin
      TheOdds:=Odd(i);
      If (TheOdds=False) Then Begin
         Write(i,' ');
         counter:=counter+i;{increase *counter* by *i*}
      End;{of if}
      Inc(i,1); {increase *i* by 1}
   End;{of while}
   WriteLn; {put the cursor to the home of the next line}
   Write('Total : ',counter);
   ReadLn; {press enter to quit}
End. {of main}
{
-=Notes=-
ODD FUNCTION:Tests if the argument is an odd number.
DECLARATION: ODD(X:LONGINT):BOOLEAN;
THE ODD FUNCTION RETURNS *TRUE* IF *X* IS AN ODD NUMBER
-=End Of Notes=-
Created By FlamingClaw 2009.07.12
}
0

Dear Sir,

Thank you for your kind help but could you mind telling me what is the meaning of the variable 'counter'?

Cheers,

2

Hi Turbomen :D
We store the sum of the even numbers in the variable named *Counter*.
When the program is running first *Counter* 's value is zero.
Counter:=0;
*TheOdds* variable is holds a *true* or *false* value changed by the built in function Odd(i) that wait longint and send back boolean,true if *i* is odd and false if *i* is even number
If *TheOdds* is false then *i* is an even number,so the *Counter*'s value will increase by *i*
Counter:=Counter+i;
Else *Counter* stays unchanged

Comments
good explination
0

Dear Sir,

Thank you for your kind expression. I really understand your meaning.

Cheers,

0

iResult:=0;
for i:2 to 20 do
if i/2 = round(i/2) then iResult=iResult+i;
eAnswer.text:=StrToInt(iResult)

0

replace line 8
if ((i1 mod 2) = 0) then Inc(iResult, i1);
with...
if not odd(i1) then Inc(iResult, i1);
which is easier to read.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.