📌  相关文章
📜  用于检查给定整数是奇数还是偶数的Java程序

📅  最后修改于: 2022-05-13 01:55:44.461000             🧑  作者: Mango

用于检查给定整数是奇数还是偶数的Java程序

偶数:能被2整除且余数为0的数称为偶数。所有以 0、2、4、6 和 8 结尾的数字都是偶数。

奇数:不能被2整除且余数为1的数称为奇数。所有以 1、3、5、7 和 9 结尾的数字都是奇数。

任何随机整数的通用说明,检查它是偶数还是奇数

Input : 13
Output: ODD

Input : 24
Output: EVEN

有多种方法可以检查给定的数字是奇数还是偶数。其中一些如下所示,从蛮力方法开始,到最佳方法为止。

  • 使用蛮力-天真的方法
  • 使用按位或
  • 使用按位与
  • 使用按位异或

1. Brute Force Naive Approach:检查除以2后的余数。能被2整除的数是偶数。

朴素方法的实现如下:

Java
// Java Program to Check if
// a Given Integer is Odd or Even
  
// Importing Classes/Files
import java.io.*;
import java.util.Scanner;
  
class GFG {
  
    // Main Driver Method
    public static void main(String[] args)
    {
        // Declare the integer variable
        int num = 10;
  
        // If condition to check if the remainder is zero
        if (num % 2 == 0) {
  
            // If remainder is zero then this number is even
            System.out.println("Entered Number is Even");
        }
        else {
  
            // If remainder is not zero then this number is
            // odd
            System.out.println("Entered Number is Odd");
        }
    }
}


Java
// Java Program to Check if
// a Given Integer is Odd or Even
  
// Importing Classes/Files
import java.util.*;
  
public class GFG {
    // Driver Main code
    public static void main(String[] args)
    {
        // Variable to be checked
        int n = 100;
  
        // Condition check
        // if n|1 if greater than n then this number is even
        if ((n | 1) > n) {
            System.out.println("Number is Even");
        }
        else {
            System.out.println("Number is Odd");
        }
    }
}


Java
// Java Program to Check if
// a Given Integer is Odd or Even
  
// Importing Classes/Files
import java.util.*;
  
public class GFG {
  
    // Driver Main Method
    public static void main(String[] args)
    {
        // Declare variable
        int n = 91;
  
        // Condition Check
        // Bitwise AND of any odd number by 1 gives 1
        if ((n & 1) == 1) {
            System.out.println("Number is Odd");
        }
        else {
            System.out.println("Number is Even");
        }
    }
}


Java
// Java Program to Check if
// a Given Integer is Odd or Even
  
// Importing Classes/Files
import java.util.*;
public class GFG {
  
    // Driver Main Method
    public static void main(String[] args)
    {
        // Declare Variable
        int num = 99;
  
        // Condition Check
        // if number^1 increments by 1 then its even number,
        // else odd
        if ((num ^ 1) == num + 1) {
            System.out.println("Number is Even");
        }
        else {
            System.out.println("Number is Odd");
        }
    }
}


输出:

Entered Number is Even

更好的方法是使用按位运算符

  • 按位或
  • 按位与或按位异或

2. 使用按位或:偶数的按位或运算加 1,则该数的值加 1,否则保持不变。

按位 OR 后跟实现的说明:

Case 1:
    Number = 12              1  1  0  0    - Representation of 12 in Binary Format
                Bitwise OR   0  0  0  1    - Representation of  1 in Binary Format
                            
                             1  1  0  1    - Representation of 13 in Binary Format
    Result- Number was even so bitwise Or by 1 increment the value by 1
            
Case 2:
    Number = 15            1  1  1  1    - Representation of 15 in Binary Format
               Bitwise OR  0  0  0  1    - Representation of  1 in Binary Format
                        
                           1  1  1  1    - Representation of 15 in Binary Format
    Result- Number was odd so bitwise Or by 1 doesn't increment the value by 1

Java

// Java Program to Check if
// a Given Integer is Odd or Even
  
// Importing Classes/Files
import java.util.*;
  
public class GFG {
    // Driver Main code
    public static void main(String[] args)
    {
        // Variable to be checked
        int n = 100;
  
        // Condition check
        // if n|1 if greater than n then this number is even
        if ((n | 1) > n) {
            System.out.println("Number is Even");
        }
        else {
            System.out.println("Number is Odd");
        }
    }
}
输出
Number is Even

3. 使用按位与:奇数加 1 的按位与运算结果为 1,因为最后一位已经设置,否则为 0。

按位与后跟实现的说明:

Case 1:
    Number = 5              0  1  0  1    - Representation of  5 in Binary Format
               Bitwise AND  0  0  0  1    - Representation of  1 in Binary Format
                          
                            0  0  0  1    - Representation of  1 in Binary Format
    Result- Number was odd so bitwise And by 1 is 1
            
Case 2:    
    Number = 8            1  0  0  0    - Representation of  8 in Binary Format
             Bitwise AND  0  0  0  1    - Representation of  1 in Binary Format
                       
                          0  0  0  0    - Representation of  0 in Binary Format
    Result- Number was even so bitwise And by 1 is 0

Java

// Java Program to Check if
// a Given Integer is Odd or Even
  
// Importing Classes/Files
import java.util.*;
  
public class GFG {
  
    // Driver Main Method
    public static void main(String[] args)
    {
        // Declare variable
        int n = 91;
  
        // Condition Check
        // Bitwise AND of any odd number by 1 gives 1
        if ((n & 1) == 1) {
            System.out.println("Number is Odd");
        }
        else {
            System.out.println("Number is Even");
        }
    }
}
输出
Number is Odd

4. 使用按位异或:偶数的按位异或运算将偶数的值加 1,否则如果值是奇数,则将数的值减 1。这是最优化的方法。

按位异或的图示,然后是实现:

Case 1:
    Number = 5              0  1  0  1    - Representation of  5 in Binary Format
               Bitwise XOR  0  0  0  1    - Representation of  1 in Binary Format
                       
                            0  1  0  0    - Representation of  4 in Binary Format
    Result- Number was odd so bitwise And by 1 decrement the value
            
Case 2:    
    Number = 8            1  0  0  0    - Representation of  8 in Binary Format
             Bitwise XOR  0  0  0  1    - Representation of  1 in Binary Format

                          1  0  0  1    - Representation of  9 in Binary Format
    Result- Number was even so bitwise And by 1 increment the value

Java

// Java Program to Check if
// a Given Integer is Odd or Even
  
// Importing Classes/Files
import java.util.*;
public class GFG {
  
    // Driver Main Method
    public static void main(String[] args)
    {
        // Declare Variable
        int num = 99;
  
        // Condition Check
        // if number^1 increments by 1 then its even number,
        // else odd
        if ((num ^ 1) == num + 1) {
            System.out.println("Number is Even");
        }
        else {
            System.out.println("Number is Odd");
        }
    }
}
输出
Number is Odd