📜  检查数字是否为奇数或偶数而无模运算符

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

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

例子 :

Input: n = 11
Output: Odd

Input: n = 10
Output: Even
 

方法1:使用循环。
想法是从布尔标志变量true开始,并将其切换n次。如果标志变量返回原始值(为真),则n为偶数。否则,n为假。

以下是此想法的实现。

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)
{
    bool isEven = true;
    for (int i=1;  i <= n; i++)
        isEven = !isEven;
    return isEven;
}
 
// Driver code
int main()
{
    int n = 101;
    isEven(n) ? cout << "Even" : cout << "Odd";
    return 0;
}


Java
// A simple Java program to
// check for even or odd
 
class GFG {
     
    // Returns true if n
    // is even, else odd
    static boolean isEven(int n)
    {
        boolean isEven = true;
         
        for (int i = 1; i <= n; i++)
            isEven = !isEven;
             
        return isEven;
    }
     
     
    // Driver Code
    public static void main(String args[])
    {
         
        int n = 101;
        if(isEven(n))
            System.out.println("Even");
        else
            System.out.println("Odd");
         
    }
}
 
// This code is contributed by Sam007


Python3
# A simple Python program to
# check for even or odd
 
# Returns true if n is even,
# else odd
def isEven(n):
    isEven = True;
    for i in range(1, n+1):
        if isEven == True:
            isEven = False;
        else:
            isEven = True;
 
    return isEven;
 
# Driver code   
n = 101;
if isEven(n) == True:
    print ("Even");
else:
    print ("Odd");
     
# This code is contributed by
# Manish Shaw (manishshaw1)


C#
// A simple C# program to check for
// even or odd
using System;
 
public class GFG {
     
    // Returns true if n is even,
    // else odd
    static bool isEven(int n)
    {
        bool isEven = true;
         
        for (int i = 1; i <= n; i++)
            isEven = !isEven;
             
        return isEven;
    }
     
    // Driver code
    public static void Main()
    {
        int n = 101;
        if(isEven(n))
            Console.Write("Even");
        else
            Console.Write("Odd");
    }
}
 
// This code is contributed by Sam007.


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)
{
    // Return true if n/2 does not result
    // in a float value.
    return ((n / 2) * 2 == n);
}
 
// Driver code
int main()
{
    int n = 101;
    isEven(n) ? cout << "Even" : cout << "Odd";
    return 0;
}


Java
// A simple Java program
// to check for even or odd
 
class GFG {
     
// Returns true if n
// is even, else odd
static boolean isEven(int n)
{
     
    // Return true if
    // n/2 does not result
    // in a float value.
    return ((n / 2) * 2 == n);
}
 
// Driver code
public static void main(String[] args)
{
    int n = 101;
    if(isEven(n) != false)
        System.out.print( "Even" );
    else
        System.out.print( "Odd" );
}
}
 
// This code is contributed by
// Smitha Dinesh Semwal.


Python3
# A simple Python 3 program
# to check for even or odd
 
# Returns true if n
# is even, else odd
def isEven(n):
 
     
    # Return true if
    # n/2 does not result
    # in a float value.
    return (int(n / 2) * 2 == n)
 
 
# Driver code
n = 101
if(isEven(n) != False):
    print("Even")
else:
    print("Odd")
 
# This code is contributed by
# Smitha Dinesh Semwal.


C#
// A simple C# program
// to check for even or odd
using System;
class GFG {
     
// Returns true if n
// is even, else odd
static bool isEven(int n)
{
     
    // Return true if
    // n/2 does not result
    // in a float value.
    return ((n / 2) * 2 == n);
}
 
// Driver code
public static void Main(String[] args)
{
    int n = 101;
    if(isEven(n) != false)
        Console.Write("Even");
    else
        Console.Write("Odd");
}
}
 
// This code is contributed by
// Smitha Dinesh Semwal.


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
// A simple Java program to check for
// even or odd
import java.io.*;
import java.util.*;
 
public class GFG {
      
    // Returns 0 if n
    // is even, else odd
    static int isEven(int n)
    {
          
        // n&1 is 1, then
        // odd, else even
        return (n & 1);
    }
      
    // Driver code
    public static void main(String args[])
    {
        int n = 101;
        if(isEven(n)==0)
            System.out.print("Even");
        else
            System.out.print("Odd");
    }
}
  
// This code is contributed
// by Manish Shaw (manishshaw1)


Python3
# A simple Python program to
# check for even or odd
# Returns 0 if n
# is even, else odd
def isEven(n) :
      
    # n&1 is 1, then
    # odd, else even
    return (n & 1);
  
# Driver code
n = 101;
if(isEven(n) == 0) :
    print ("Even");
else :
    print ("Odd");
      
# This code is contributed
# by Manish Shaw (manishshaw1)


C#
// A simple C# program to check for
// even or odd
using System;
using System.Collections.Generic;
 
class GFG {
     
    // Returns 0 if n
    // is even, else odd
    static int isEven(int n)
    {
         
        // n&1 is 1, then
        // odd, else even
        return (n & 1);
    }
     
    // Driver code
    public static void Main()
    {
        int n = 101;
        if(isEven(n)==0)
            Console.Write("Even");
        else
            Console.Write("Odd");
    }
}
 
// This code is contributed
// by Manish Shaw (manishshaw1)


PHP


输出:
Odd

方法2:通过乘以2并除以2。如果结果与输入相同,则将其除以2,然后乘以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 true if n/2 does not result
    // in a float value.
    return ((n / 2) * 2 == n);
}
 
// Driver code
int main()
{
    int n = 101;
    isEven(n) ? cout << "Even" : cout << "Odd";
    return 0;
}

Java

// A simple Java program
// to check for even or odd
 
class GFG {
     
// Returns true if n
// is even, else odd
static boolean isEven(int n)
{
     
    // Return true if
    // n/2 does not result
    // in a float value.
    return ((n / 2) * 2 == n);
}
 
// Driver code
public static void main(String[] args)
{
    int n = 101;
    if(isEven(n) != false)
        System.out.print( "Even" );
    else
        System.out.print( "Odd" );
}
}
 
// This code is contributed by
// Smitha Dinesh Semwal.

Python3

# A simple Python 3 program
# to check for even or odd
 
# Returns true if n
# is even, else odd
def isEven(n):
 
     
    # Return true if
    # n/2 does not result
    # in a float value.
    return (int(n / 2) * 2 == n)
 
 
# Driver code
n = 101
if(isEven(n) != False):
    print("Even")
else:
    print("Odd")
 
# This code is contributed by
# Smitha Dinesh Semwal.

C#

// A simple C# program
// to check for even or odd
using System;
class GFG {
     
// Returns true if n
// is even, else odd
static bool isEven(int n)
{
     
    // Return true if
    // n/2 does not result
    // in a float value.
    return ((n / 2) * 2 == n);
}
 
// Driver code
public static void Main(String[] args)
{
    int n = 101;
    if(isEven(n) != false)
        Console.Write("Even");
    else
        Console.Write("Odd");
}
}
 
// This code is contributed by
// Smitha Dinesh Semwal.

的PHP


Java脚本


输出 :
Odd

方法3:使用按位运算符&。
更好的解决方案是使用按位运算运算符。我们需要检查最后一位是否为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

// A simple Java program to check for
// even or odd
import java.io.*;
import java.util.*;
 
public class GFG {
      
    // Returns 0 if n
    // is even, else odd
    static int isEven(int n)
    {
          
        // n&1 is 1, then
        // odd, else even
        return (n & 1);
    }
      
    // Driver code
    public static void main(String args[])
    {
        int n = 101;
        if(isEven(n)==0)
            System.out.print("Even");
        else
            System.out.print("Odd");
    }
}
  
// This code is contributed
// by Manish Shaw (manishshaw1)

Python3

# A simple Python program to
# check for even or odd
# Returns 0 if n
# is even, else odd
def isEven(n) :
      
    # n&1 is 1, then
    # odd, else even
    return (n & 1);
  
# Driver code
n = 101;
if(isEven(n) == 0) :
    print ("Even");
else :
    print ("Odd");
      
# This code is contributed
# by Manish Shaw (manishshaw1)

C#

// A simple C# program to check for
// even or odd
using System;
using System.Collections.Generic;
 
class GFG {
     
    // Returns 0 if n
    // is even, else odd
    static int isEven(int n)
    {
         
        // n&1 is 1, then
        // odd, else even
        return (n & 1);
    }
     
    // Driver code
    public static void Main()
    {
        int n = 101;
        if(isEven(n)==0)
            Console.Write("Even");
        else
            Console.Write("Odd");
    }
}
 
// This code is contributed
// by Manish Shaw (manishshaw1)

的PHP


输出 :
Odd