Current location - Training Enrollment Network - Mathematics courses - How to take the 17 number of 18 digits and judge whether it is even or odd?
How to take the 17 number of 18 digits and judge whether it is even or odd?
1. Mathematical definition: In mathematics, anything divisible by 2 is defined as an even number. On the contrary, it's strange. Second, algorithm analysis: According to the mathematical definition and some knowledge of C language, we can get many methods to judge the parity of an integer, such as: 1, the most common and intuitive method. Take the remainder of 2. If it is 0, it means divisibility, and it is even. Otherwise, it's strange. In other words, n%2==0 is an even number. N%2== 1 is an odd number. Because C language stipulates that 0 is false and 1 is true in logical operation, judging parity can be simplified as follows

Else printf("n is an even number \ n "); 2. The most effective method.

Computers are all stored in binary, so judging parity can actually judge the last bit of binary. So it can be judged by more effective bit operation:

Else printf("n is an even number \ n "); 3. Other methods:

According to all kinds of mathematical reasoning and C language operation, there are many other methods, but they are not commonly used, just for understanding. Here are a few examples: