Answered by:
Sum of even and odd numbers  urgently!
Question

I'm working on a programme that shows the sum of even numbers, and another programme that shows the sum of odd numbers. I don't know how to make them. It's very urgent and I need them. If anyone knows these programmes, please tell me. I'll be very thankful. But I need them very fast and I need both of them. Thanks in advance.
Answers

I'm sorry, but this is not the right answer. Here's the right answer :
TextWindow.WriteLine("Enter a number : ") n = TextWindow.ReadNumber() sum = 0 For i = 2 To n Step 2 (for even numbers and For i = 1 for odd numbers) sum = sum + i EndFor TextWindow.WriteLine("The sum is : " + sum)
But, anyway, thanks for the answer above.
 Marked as answer by Ed Price  MSFTMicrosoft employee, Owner Friday, October 19, 2012 5:53 PM

It was not my intention that scrap of mine be a productiondeliveryallwrappedupforpresent code! :P
Otherwise, I'd not leave such lines as:
TextWindow.WriteLine(i  1 + " is even") TextWindow.WriteLine(i + " is odd")
It'd be a Subroutine snippet which would silently calculate and return the required work, as I have done sometimes in this forum before!
It was just a code for study for LovelyBone. He needs to adapt that to his intended program!
Now for the SB's default value, just look at the code below and tell me what you think of my conclusion:
a= a TextWindow.WriteLine(a) ' < Empty default value "" a= a+1 ' < a = "" + 1 ( a concatenation operation) TextWindow.WriteLine(a) a= a+1 ' < a = 1 + 1 ( an addition operation) TextWindow.WriteLine(a)
Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)
 Edited by GoToLoopEditor Tuesday, June 5, 2012 6:02 PM
 Marked as answer by Ed Price  MSFTMicrosoft employee, Owner Friday, October 19, 2012 5:53 PM

HEHE
The few times I program in C, I use the exactly minimum type of declaration for each variable. Like #Define or const, signed or unsigned, char or int, etc. I'm very meticulous and embrace the language I'm using at the moment!
So, when I'm on SB, I really am in SB! Since SB got a very peculiar way of doing variables and array indexes, I try to use its special qualities.
Perhaps the only other language I know of which has a similar approach is Lua. I'm intending to study it soon. @_@
And it's better this way; it's like I'm taking a vacation from too much strict languages. :P
Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)
 Edited by GoToLoopEditor Tuesday, June 5, 2012 7:59 PM
 Proposed as answer by Ed Price  MSFTMicrosoft employee, Owner Thursday, October 11, 2012 6:18 AM
 Marked as answer by Ed Price  MSFTMicrosoft employee, Owner Friday, October 19, 2012 5:52 PM

I wasn't giving the full answer, just a guide to get started, I said "I leave it to you to sum (add up) the odd or even numbers, hint you will need new variables for each of the even and number totals."
Well done.
To sum both odd and even, perhaps this:
TextWindow.WriteLine("Enter a number to sum odd and even numbers: ")
n = TextWindow.ReadNumber()
sumOdd = 0
sumEven = 0
For i = 1 To n Step 2
sumOdd = sumOdd + i
If (i < n) Then
sumEven = sumEven + i +1
EndIf
EndFor
TextWindow.WriteLine("The sum of odd numbers is : " + sumOdd)
TextWindow.WriteLine("The sum of even numbers is : " + sumEven) Edited by litdevModerator Tuesday, June 5, 2012 11:02 AM
 Marked as answer by Ed Price  MSFTMicrosoft employee, Owner Friday, October 19, 2012 5:53 PM
All replies



First we can start with a For loop that sets i from 1 to 100
For i = 1 To 100 TextWindow.WriteLine(i) EndFor
I added the WriteLine to output the results to see what it is doing  debugging it.
We know that even or odd numbers are every second number, so what we want is the loop to go up in steps of 2, we can do this, again with the debugging to see what it is doing. Note the Step 2 part.
For i = 1 To 100 Step 2 TextWindow.WriteLine(i) EndFor
Notice these are all the odd numbers, the even ones are just 1 plus each odd number
For i = 1 To 100 Step 2 odd = i even = odd+1 TextWindow.WriteLine(odd+" is odd") TextWindow.WriteLine(even+" is even") EndFor
I added new variables odd and even to store the odd and even numbers, and kept the debugging info to see what it is doing.
I leave it to you to sum (add up) the odd or even numbers, hint you will need new variables for each of the even and number totals.

I'm sorry, but this is not the right answer. Here's the right answer :
TextWindow.WriteLine("Enter a number : ") n = TextWindow.ReadNumber() sum = 0 For i = 2 To n Step 2 (for even numbers and For i = 1 for odd numbers) sum = sum + i EndFor TextWindow.WriteLine("The sum is : " + sum)
But, anyway, thanks for the answer above.
 Marked as answer by Ed Price  MSFTMicrosoft employee, Owner Friday, October 19, 2012 5:53 PM

I wasn't giving the full answer, just a guide to get started, I said "I leave it to you to sum (add up) the odd or even numbers, hint you will need new variables for each of the even and number totals."
Well done.
To sum both odd and even, perhaps this:
TextWindow.WriteLine("Enter a number to sum odd and even numbers: ")
n = TextWindow.ReadNumber()
sumOdd = 0
sumEven = 0
For i = 1 To n Step 2
sumOdd = sumOdd + i
If (i < n) Then
sumEven = sumEven + i +1
EndIf
EndFor
TextWindow.WriteLine("The sum of odd numbers is : " + sumOdd)
TextWindow.WriteLine("The sum of even numbers is : " + sumEven) Edited by litdevModerator Tuesday, June 5, 2012 11:02 AM
 Marked as answer by Ed Price  MSFTMicrosoft employee, Owner Friday, October 19, 2012 5:53 PM

This is my version:
TextWindow.WriteLine("Type counter limit: ") n = TextWindow.ReadNumber() For i = 1 To n Step 2 TextWindow.WriteLine(i  1 + " is even") TextWindow.WriteLine(i + " is odd") evenSum = evenSum + i  1 oddSum = oddSum + i EndFor If i  n = 1 Then evenSum = evenSum + i  1 EndIf TextWindow.WriteLine("From 0 up to " + n) TextWindow.WriteLine("Even Sum = " + evenSum) TextWindow.WriteLine("Odd Sum = " + oddSum)
Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)
 Edited by GoToLoopEditor Tuesday, June 5, 2012 11:24 AM


Or perhaps to the empty void > ""; which I believe it is the true SB's variable's starting value ^_^
Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)
 Edited by GoToLoopEditor Tuesday, June 5, 2012 12:59 PM

Yep, that would do great since the empty string "" deletes a variable and its default numerical value is zero  that's why it works OK without initialisation. The problems would start if you reused the code without reinitialising, perhaps if you asked the user if they wanted to do it again and return to the start.

It was not my intention that scrap of mine be a productiondeliveryallwrappedupforpresent code! :P
Otherwise, I'd not leave such lines as:
TextWindow.WriteLine(i  1 + " is even") TextWindow.WriteLine(i + " is odd")
It'd be a Subroutine snippet which would silently calculate and return the required work, as I have done sometimes in this forum before!
It was just a code for study for LovelyBone. He needs to adapt that to his intended program!
Now for the SB's default value, just look at the code below and tell me what you think of my conclusion:
a= a TextWindow.WriteLine(a) ' < Empty default value "" a= a+1 ' < a = "" + 1 ( a concatenation operation) TextWindow.WriteLine(a) a= a+1 ' < a = 1 + 1 ( an addition operation) TextWindow.WriteLine(a)
Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)
 Edited by GoToLoopEditor Tuesday, June 5, 2012 6:02 PM
 Marked as answer by Ed Price  MSFTMicrosoft employee, Owner Friday, October 19, 2012 5:53 PM

I didn't mean to criticize your code, just mention the initialisation.
As you know operations in SB are context dependent, so either method is fine  its only a style preference for me to use 0 rather than "" since I know this variable will be a number, perhaps due to most languages being strongly typed I am biased that way, but no big deal at all.

HEHE
The few times I program in C, I use the exactly minimum type of declaration for each variable. Like #Define or const, signed or unsigned, char or int, etc. I'm very meticulous and embrace the language I'm using at the moment!
So, when I'm on SB, I really am in SB! Since SB got a very peculiar way of doing variables and array indexes, I try to use its special qualities.
Perhaps the only other language I know of which has a similar approach is Lua. I'm intending to study it soon. @_@
And it's better this way; it's like I'm taking a vacation from too much strict languages. :P
Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)
 Edited by GoToLoopEditor Tuesday, June 5, 2012 7:59 PM
 Proposed as answer by Ed Price  MSFTMicrosoft employee, Owner Thursday, October 11, 2012 6:18 AM
 Marked as answer by Ed Price  MSFTMicrosoft employee, Owner Friday, October 19, 2012 5:52 PM