Board index » delphi » "Odd" TP question

"Odd" TP question

Hello
I am new at programing and there is one review question in my TP manual that
has been bothering me for the past couple of days, ive tried many different
things and am wondering why they dont work. maybe someone can help:

Question:

Write a problem that finds the sum of all odd numbers between 1 and a number
entered by the user. (Hint:  You will need a statement inside the loop to
check each number, using MOD 2 to determine if it is odd.)

would someone please solve this for me and explain it. Thank you.
I am having trouble making the program recognize odd numbers, the output
returned is usually 0 1 0 1 0 1 and so on...

wholyCow
wholy...@tekkenking.com

 

Re:"Odd" TP question


Quote
On Thu, 12 Nov 1998 02:33:58 GMT, "-" <1...@spamers-suck.com> wrote:
>Hello
>I am new at programing and there is one review question in my TP manual that
>has been bothering me for the past couple of days, ive tried many different
>things and am wondering why they dont work. maybe someone can help:

>Question:

>Write a problem that finds the sum of all odd numbers between 1 and a number
>entered by the user. (Hint:  You will need a statement inside the loop to
>check each number, using MOD 2 to determine if it is odd.)

>would someone please solve this for me and explain it. Thank you.
>I am having trouble making the program recognize odd numbers, the output
>returned is usually 0 1 0 1 0 1 and so on...

>wholyCow
>wholy...@tekkenking.com

Since this question is already answered elsewhere in this thread, just
another question from me: Why is it that if you get a question like
this for homework, your teacher is never really happy if you _really_
solve the problem:

Program Test;

Var
  Upper: Integer;
  N    : Integer;

Begin
  Write   ('Enter upper limit: ');
  Readln  (Upper);
  N := Upper div 2;
  If (Upper mod 2) = 1 Then Inc (N);
  Writeln ('Sum of odd numbers from 1 to ', Upper, ' is: ', N * N);
End.

Klaas
---
Sorry, couldn't resist it :-)

Other Threads