如何判斷一個數的奇偶性

import java.util.Scanner; java


public class EvenOddNumber {
public static void main(String args[]){              
       
        //scanner to get input from user
        Scanner console = new Scanner(System.in);
       
        System.out.printf("Enter any number : ");
       
        //return the user input as integer
        int number = console.nextInt();
        long start = System.currentTimeMillis();
        //if remainder is zero than even number
        for (int i = 0; i < 1000000000; i++){
         int j = number%2;
        }          
        long end = System.currentTimeMillis();
        System.out.println(end - start);
        //Finding Even and Odd number using Bitwise AND operator in Java.
       
        System.out.printf("Finding number if its even or odd using bitwise AND operator %n");
       
        //For Even numbers
        //XXX0
        //0001 AND
        //0000
        start = System.currentTimeMillis();
        for (int i = 0; i < 1000000000; i++){
         int j = number&1;
        }      
        end = System.currentTimeMillis();
        System.out.println(end - start);
        System.out.printf("Finding number if its even or odd using divid operator %n");
        start = System.currentTimeMillis();
        for (int i = 0; i < 1000000000; i++){
         int j = number/1;
        }      
        end = System.currentTimeMillis();
        System.out.println(end - start);
    } rem

} get

相關文章
相關標籤/搜索