📌  相关文章
📜  检查给定数字是偶数还是奇数

📅  最后修改于: 2021-05-25 04:02:23             🧑  作者: Mango

给定一个数字,检查它是偶数还是奇数。

例子 :

Input: 2 
Output: even

Input: 5
Output: odd

一种简单的解决方案是在除以2之后找到余数。

C++
// A simple C++ program to
// check for even or odd
#include 
using namespace std;
 
// Returns true if n is
// even, else odd
bool isEven(int n) { return (n % 2 == 0); }
 
// Driver code
int main()
{
    int n = 101;
    isEven(n) ? cout << "Even" : cout << "Odd";
 
    return 0;
}


Java
// Java program program to
// check for even or odd
 
class GFG
{
    // Returns true if n is even, else odd
    public static boolean isEven(int n)
    {
        return (n % 2 == 0);
    }
     
    // Driver code
    public static void main(String[] args)
    {
        int n = 101;
        if(isEven(n) == true)
            System.out.print("Even");
        else
            System.out.print("Odd");
    }
}
 
// This code is contributed by rishabh_jain


Python3
# A simple Python3 code
# to check for even or odd
 
# Returns true if n is even, else odd
def isEven(n):
    return (n % 2 == 0)
     
# Driver code
n = 101
print("Even" if isEven(n) else "Odd")
 
# This code is contributed by "Sharad_Bhardwaj".


C#
// C# program program to
// check for even or odd
using System;
 
class GFG
{
    // Returns true if n is even, else odd
    public static bool isEven(int n)
    {
        return (n % 2 == 0);
    }
     
    // Driver code
    public static void Main()
    {
        int n = 101;
        if(isEven(n) == true)
            Console.WriteLine("Even");
        else
            Console.WriteLine("Odd");
    }
}
 
// This code is contributed by vt_m


PHP


Javascript


C++
// A simple C++ program to
// check for even or odd
#include 
using namespace std;
 
// Returns true if n is
// even, else odd
bool isEven(int n)
{
     
// n & 1 is 1, then
// odd, else even
return (!(n & 1));
}
 
// Driver code
int main()
{
int n = 101;
isEven(n)? cout << "Even" :
           cout << "Odd";
 
return 0;
}


Java
// Java program program to
// check for even or odd
 
class GFG
{
    // Returns true if n
    // is even, else odd
    public static boolean isEven(int n)
    {
        if((n & 1) == 0)
            return true;
        else
            return false;
    }
     
    // Driver code
    public static void main(String[] args)
    {
        int n = 101;
        if(isEven(n) == true)
            System.out.print("Even");
        else
            System.out.print("Odd");
    }
}
 
// This code is contributed by rishabh_jain


Python3
# A Python3 code program
# to check for even or odd
 
# Returns true if n is even, else odd
def isEven(n):
     
    # n&1 is 1, then odd, else even
    return (not(n & 1))
     
# Driver code
n = 101;
print("Even" if isEven(n) else "Odd")
 
# This code is contributed by "Sharad_Bhardwaj".


C#
// C# program program to
// check for even or odd
using System;
 
class GFG
{
    // Returns true if n
    // is even, else odd
    public static bool isEven(int n)
    {
        if((n & 1) == 0)
            return true;
        else
            return false;
    }
     
    // Driver code
    public static void Main()
    {
        int n = 101;
        if(isEven(n) == true)
            Console.WriteLine("Even");
        else
            Console.WriteLine("Odd");
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


输出 :

Odd

时间复杂度: O(1)

辅助空间: O(1)
更好的解决方案是使用按位运算运算符。我们需要检查最后一位是否为1。如果最后一位为1,则数字为奇数,否则始终为偶数。
解释:

input : 5    // odd
   00000101              
 & 00000001                
--------------                
   00000001       
--------------

input : 8     //even
   00001000              
 & 00000001                 
--------------               
   00000000        
--------------

以下是该想法的实现。

C++

// A simple C++ program to
// check for even or odd
#include 
using namespace std;
 
// Returns true if n is
// even, else odd
bool isEven(int n)
{
     
// n & 1 is 1, then
// odd, else even
return (!(n & 1));
}
 
// Driver code
int main()
{
int n = 101;
isEven(n)? cout << "Even" :
           cout << "Odd";
 
return 0;
}

Java

// Java program program to
// check for even or odd
 
class GFG
{
    // Returns true if n
    // is even, else odd
    public static boolean isEven(int n)
    {
        if((n & 1) == 0)
            return true;
        else
            return false;
    }
     
    // Driver code
    public static void main(String[] args)
    {
        int n = 101;
        if(isEven(n) == true)
            System.out.print("Even");
        else
            System.out.print("Odd");
    }
}
 
// This code is contributed by rishabh_jain

Python3

# A Python3 code program
# to check for even or odd
 
# Returns true if n is even, else odd
def isEven(n):
     
    # n&1 is 1, then odd, else even
    return (not(n & 1))
     
# Driver code
n = 101;
print("Even" if isEven(n) else "Odd")
 
# This code is contributed by "Sharad_Bhardwaj".

C#

// C# program program to
// check for even or odd
using System;
 
class GFG
{
    // Returns true if n
    // is even, else odd
    public static bool isEven(int n)
    {
        if((n & 1) == 0)
            return true;
        else
            return false;
    }
     
    // Driver code
    public static void Main()
    {
        int n = 101;
        if(isEven(n) == true)
            Console.WriteLine("Even");
        else
            Console.WriteLine("Odd");
    }
}
 
// This code is contributed by vt_m.

的PHP


Java脚本


输出 :

Odd