Knowledge point:
1. Odd Number and even number judgment: (1) Remove the remainder (%) (2) and Phase 1 (&) to determine whether it is 0
2. You can write one of the two methods, but write two to increase readability.
Problem:
A simple method is required to test a value to determine whether it is an odd or even number.
Solution
1 using system; 2 using system. collections. generic; 3 using system. LINQ; 4 using system. text; 5 using system. threading. tasks; 6 7 namespace _ 06 Test parity 8 {9 class program10 {11 static void main (string [] ARGs) 12 {13 console. writeline ("enter a number:"); 14 VaR value = console. readline (); 15 bool valueprop = iseven (convert. toint32 (value); 16 if (valueprop) 17 {18 console. writeline ("is even"); 19} 20 else21 {22 conso Le. writeline ("is odd"); 23} 24 console. readkey (); 25} 26 27 // Method 1: remove the remaining 28 public static bool iseven (INT intvalue) 29 {30 return (intvalue % 2 = 0) from 2 ); 31} 32 33 public static bool isodd (INT intvalue) 34 {35 return (intvalue % 2 = 1); 36} 37 38 // Method 2: the odd number always sets its second bit to 1. Therefore, it can be connected to phase 1 and (and). If it is 0, it is an even number. If it is 1, it is an odd number 39 public static bool isodd (INT intvalue) 40 {41 return (! Iseven (intvalue); 42} 43 44 public static bool iseven (INT intvalue) 45 {46 Return (intvalue & 1) = 0); 47} 48} 49}View code
Verification Result
1. Input 5 and the result is isodd.
2. Input 6 and the result is iseven.
1.5 test parity