Wed, 18 Jun 1902 08:00:00 GMT
"Odd" TP question
Quote- 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...
The end result appears to request that you only provide one number. While I won't give away a solution, perhaps some hints will help. First you need three variables: one to hold the sum, one to tell how high you want to count, and one as an index for a loop. Let's respectively name them sum, upper, and index. {upper is entered from user} initialize sum to 0; for index := 1 to upper do if index is an odd number then add it to sum; report results of sum to user; That's all you have to do. The most difficult part here (which shouldn't even be that difficult) is figuring out how to test for odd/even using "mod 2". Bonus hint: In case you didn't know, the "mod" operator returns the remainder of division. When you learned elementary math, what can you determine by dividing a number by 2? Quote> wholyCow > wholy...@tekkenking.com
-- Scott Earnest | SPAM protection in effect. Remove | setech@_ix.netcom.com | "_" as needed for true addresses. | earnests@_homenet.lm.com | UIN:1136443 EFnet:pale_blue | sinykal@_{*word*104}space.org | URL: http://www.netcom.com/~setech |
|