Answered by:
modular arithmetic
Question
Answers

in this instance,, yes. Want to multiply by 1 alternate times.
If you want to alternate between even/odd then you don't want mod 1, you want mod 2. Mod 1 will return the decimal portion of a number. For any integer input it will always return 0. For example, 3.14159 mod 1 will return 0.14159. Mod 2 will alternate between 0 and 1, (for integer inputs) which is what you want.
thx What was wrong w '%' ? Marked as answer by bob witte Monday, July 26, 2010 9:00 PM
All replies





in this instance,, yes. Want to multiply by 1 alternate times.
If you want to alternate between even/odd then you don't want mod 1, you want mod 2. Mod 1 will return the decimal portion of a number. For any integer input it will always return 0. For example, 3.14159 mod 1 will return 0.14159. Mod 2 will alternate between 0 and 1, (for integer inputs) which is what you want. Proposed as answer by John Anthony Oliver Tuesday, July 27, 2010 12:01 AM

in this instance,, yes. Want to multiply by 1 alternate times.
If you want to alternate between even/odd then you don't want mod 1, you want mod 2. Mod 1 will return the decimal portion of a number. For any integer input it will always return 0. For example, 3.14159 mod 1 will return 0.14159. Mod 2 will alternate between 0 and 1, (for integer inputs) which is what you want.
thx What was wrong w '%' ? Marked as answer by bob witte Monday, July 26, 2010 9:00 PM


Other than being the modulo operator for C...nothing is intrinsically wrong with %. In BASIC, % is shorthand for Integer types.
Another quick way to differentiate odd from even is to AND with 1. (Even numbers return 0 and odd ones return 1. There is no division, and you do not need to test for zero to avoid an exception.)


Hi Bob Witte,
MOD gives the remainder.
So 3 Mod 2 = 1
4 Mod 2 = 0
Anyway, with one button on a Form try this slight alteration on your code please.
The output is.>>
0 + 0 = 0 is even.
0 + 1 = 1 is odd.
0 + 2 = 2 is even.
0 + 3 = 3 is odd.
1 + 0 = 1 is odd.
1 + 1 = 2 is even.
1 + 2 = 3 is odd.
1 + 3 = 4 is even.
2 + 0 = 2 is even.
2 + 1 = 3 is odd.
2 + 2 = 4 is even.
2 + 3 = 5 is odd.
3 + 0 = 3 is odd.
3 + 1 = 4 is even.
3 + 2 = 5 is odd.
3 + 3 = 6 is even.Option Strict On Imports System.Environment Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim sb As New System.Text.StringBuilder Dim x As Integer For r As Integer = 0 To 3 For c As Integer = 0 To 3 x = (r + c) Mod 2 If x = 1 Then sb.Append(r.ToString & " + " & c.ToString & " = " & (r + c).ToString & " is odd." & NewLine) Else sb.Append(r.ToString & " + " & c.ToString & " = " & (r + c).ToString & " is even." & NewLine) End If Next Next MessageBox.Show(sb.ToString) End Sub End Class
Regards, John