如何使用C#查找数字是偶数还是奇数? [英] How to find whether a number is even or odd using C#?
问题描述
我知道已经有一种方法可以使用模数来确定一个数字是偶数还是奇数(查找数字是偶数还是奇数的最快方法是什么?)但是我想知道是否有 C# 函数像 Math.Even 或 Math.Odd.使用模数是唯一的方法吗?
I know that there is already a way to find if a number is even or odd using the modulus (What is the fastest way to find if a number is even or odd?) however I was wondering if there is a C# function like Math.Even or Math.Odd. Is the only way to do this by using a modulus?
(我知道这听起来像一个愚蠢的问题,但我敢打赌我的老师在 C# 中有一个内置功能可以做到这一点,他声称没有......)
(I know this sounds like a stupid question, but I bet my teacher that there was a built in feature to do this in C#, he claims there isn't....)
推荐答案
这可能算作作弊,但如果您使用 BigInteger,它有一个 IsEven 方法.
It may count as cheating, but if you use BigInteger, it has an IsEven method.
如MSDN所述,调用此方法相当于:
As stated in MSDN, calling this method is equivalent to:
value % 2 == 0;
参考:
这篇关于如何使用C#查找数字是偶数还是奇数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!