# 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.
Friday, June 1, 2012 3:37 PM

• I'm sorry, but this is not the right answer. Here's the right answer :

```TextWindow.WriteLine("Enter a number : ")
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.

Tuesday, June 5, 2012 10:37 AM
• It was not my intention that scrap of mine be a production-delivery-all-wrapped-up-for-present 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! (^_^)

Tuesday, June 5, 2012 5:56 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! (^_^)

Tuesday, June 5, 2012 7: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 = 0sumEven = 0For i = 1 To n Step 2  sumOdd = sumOdd + i  If (i < n) Then    sumEven = sumEven + i +1  EndIfEndForTextWindow.WriteLine("The sum of odd numbers is : " + sumOdd)TextWindow.WriteLine("The sum of even numbers is : " + sumEven)`

Tuesday, June 5, 2012 10:55 AM

### All replies

• What code do you have so far?
Friday, June 1, 2012 5:49 PM
• This is a code I've done, but it's not the solution of the problem. I'm not good at this, but fine. Here it is :

`For i = 1 To 100i = i + 1 (for even numbers) or i = i + 2 (for odd numbers)s = 0EndForFor i = 1 To 3s = s + iEndFor`

Saturday, June 2, 2012 10:55 AM
• 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.

Saturday, June 2, 2012 11:27 AM
• I'm sorry, but this is not the right answer. Here's the right answer :

```TextWindow.WriteLine("Enter a number : ")
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.

Tuesday, June 5, 2012 10:37 AM
• 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 = 0sumEven = 0For i = 1 To n Step 2  sumOdd = sumOdd + i  If (i < n) Then    sumEven = sumEven + i +1  EndIfEndForTextWindow.WriteLine("The sum of odd numbers is : " + sumOdd)TextWindow.WriteLine("The sum of even numbers is : " + sumEven)`

Tuesday, June 5, 2012 10:55 AM
• This is my version:

```TextWindow.WriteLine("Type counter limit: ")

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! (^_^)

Tuesday, June 5, 2012 11:23 AM
• GoToLoop, While the compiler doesn't insist on it, it is best to initialize evenSum and oddSum to zero initially.
Tuesday, June 5, 2012 11:39 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! (^_^)

Tuesday, June 5, 2012 12:58 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 re-used the code without re-initialising, perhaps if you asked the user if they wanted to do it again and return to the start.
Tuesday, June 5, 2012 4:32 PM
• It was not my intention that scrap of mine be a production-delivery-all-wrapped-up-for-present 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! (^_^)

Tuesday, June 5, 2012 5:56 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.

Tuesday, June 5, 2012 6:42 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! (^_^)

Tuesday, June 5, 2012 7:52 PM