943,870 Members | Top Members by Rank

Ad:
Jul 12th, 2009
0

Please help me on Delphi.

Expand Post »
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,
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster
turbomen is offline Offline
113 posts
since Feb 2009
Jul 12th, 2009
1

Re: Please help me on Delphi.

delphi Syntax (Toggle Plain Text)
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. Var
  3. i1, iResult: Integer;
  4. begin
  5. iResult := 0;
  6. for i1 := 2 to 20 do
  7. begin
  8. if ((i1 mod 2) = 0) then Inc(iResult, i1);
  9. end;
  10. ShowMessage(IntToStr(iResult));
  11. end;
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Jul 12th, 2009
1

Re: Please help me on Delphi.

Hi Turbomen
If you want to create a console application then this is also a solution
delphi Syntax (Toggle Plain Text)
  1.  
  2. Program EvenOrNot;
  3.  
  4. {$APPTYPE CONSOLE}
  5.  
  6. Uses
  7. SysUtils;
  8.  
  9. Var i,counter:LongInt;
  10. TheOdds:Boolean; {true or false}
  11. Begin
  12. i:=2;
  13. counter:=0;
  14. Write('The even numbers are: ');
  15. While i<=21 Do Begin
  16. TheOdds:=Odd(i);
  17. If (TheOdds=False) Then Begin
  18. Write(i,' ');
  19. counter:=counter+i;{increase *counter* by *i*}
  20. End;{of if}
  21. Inc(i,1); {increase *i* by 1}
  22. End;{of while}
  23. WriteLn; {put the cursor to the home of the next line}
  24. Write('Total : ',counter);
  25. ReadLn; {press enter to quit}
  26. End. {of main}
  27. {
  28. -=Notes=-
  29. ODD FUNCTION:Tests if the argument is an odd number.
  30. DECLARATION: ODD(X:LONGINT):BOOLEAN;
  31. THE ODD FUNCTION RETURNS *TRUE* IF *X* IS AN ODD NUMBER
  32. -=End Of Notes=-
  33. Created By FlamingClaw 2009.07.12
  34. }
Last edited by FlamingClaw; Jul 12th, 2009 at 3:19 pm.
Reputation Points: 132
Solved Threads: 138
Posting Pro
FlamingClaw is offline Offline
559 posts
since Feb 2009
Jul 14th, 2009
0

Re: Please help me on Delphi.

Dear Sir,

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

Cheers,
Reputation Points: 10
Solved Threads: 0
Junior Poster
turbomen is offline Offline
113 posts
since Feb 2009
Jul 14th, 2009
2

Re: Please help me on Delphi.

Hi Turbomen
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
Reputation Points: 132
Solved Threads: 138
Posting Pro
FlamingClaw is offline Offline
559 posts
since Feb 2009
Jul 15th, 2009
0

Re: Please help me on Delphi.

Dear Sir,

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

Cheers,
Reputation Points: 10
Solved Threads: 0
Junior Poster
turbomen is offline Offline
113 posts
since Feb 2009
Jul 22nd, 2009
0

Re: Please help me on Delphi.

iResult:=0;
for i:2 to 20 do
if i/2 = round(i/2) then iResult=iResult+i;
eAnswer.text:=StrToInt(iResult)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
marsheng is offline Offline
15 posts
since May 2007
Sep 11th, 2009
0

Re: Please help me on Delphi.

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
EdFallon is offline Offline
1 posts
since Sep 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Pascal and Delphi Forum Timeline: find replace
Next Thread in Pascal and Delphi Forum Timeline: How to smaller my attach image to Database?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC