1.5 test parity

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.