Please help me on Delphi.

Please support our Pascal and Delphi advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Feb 2009
Posts: 93
Reputation: turbomen is an unknown quantity at this point 
Solved Threads: 0
turbomen turbomen is offline Offline
Junior Poster in Training

Please help me on Delphi.

 
0
  #1
Jul 12th, 2009
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,
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,464
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 629
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Please help me on Delphi.

 
1
  #2
Jul 12th, 2009
  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;
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 473
Reputation: FlamingClaw will become famous soon enough FlamingClaw will become famous soon enough 
Solved Threads: 114
FlamingClaw's Avatar
FlamingClaw FlamingClaw is offline Offline
Posting Pro in Training

Re: Please help me on Delphi.

 
1
  #3
Jul 12th, 2009
Hi Turbomen
If you want to create a console application then this is also a solution
  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.
Be a good part of the community.Don't be ungrateful.
If you ask something on the forum and you got the right answer then mark as solved!
If my opinion helped to you a lot then sometimes give reputation point to me.
I'm just a pascal programmer from Hungary.
Farewell...
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 93
Reputation: turbomen is an unknown quantity at this point 
Solved Threads: 0
turbomen turbomen is offline Offline
Junior Poster in Training

Re: Please help me on Delphi.

 
0
  #4
Jul 14th, 2009
Dear Sir,

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

Cheers,
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 473
Reputation: FlamingClaw will become famous soon enough FlamingClaw will become famous soon enough 
Solved Threads: 114
FlamingClaw's Avatar
FlamingClaw FlamingClaw is offline Offline
Posting Pro in Training

Re: Please help me on Delphi.

 
2
  #5
Jul 14th, 2009
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
Be a good part of the community.Don't be ungrateful.
If you ask something on the forum and you got the right answer then mark as solved!
If my opinion helped to you a lot then sometimes give reputation point to me.
I'm just a pascal programmer from Hungary.
Farewell...
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 93
Reputation: turbomen is an unknown quantity at this point 
Solved Threads: 0
turbomen turbomen is offline Offline
Junior Poster in Training

Re: Please help me on Delphi.

 
0
  #6
Jul 15th, 2009
Dear Sir,

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

Cheers,
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 15
Reputation: marsheng is an unknown quantity at this point 
Solved Threads: 0
marsheng marsheng is offline Offline
Newbie Poster

Re: Please help me on Delphi.

 
0
  #7
Jul 22nd, 2009
iResult:=0;
for i:2 to 20 do
if i/2 = round(i/2) then iResult=iResult+i;
eAnswer.text:=StrToInt(iResult)
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 1
Reputation: EdFallon is an unknown quantity at this point 
Solved Threads: 0
EdFallon EdFallon is offline Offline
Newbie Poster

Re: Please help me on Delphi.

 
0
  #8
Sep 11th, 2009
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Pascal and Delphi Forum


Views: 822 | Replies: 7
Thread Tools Search this Thread



Tag cloud for Pascal and Delphi
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC