Judge whether an integer is a small solution with an odd number.

Source: Internet
Author: User

Use n % 2 = 1 to determine whether it is an odd number:

Import Java. io. bufferedinputstream; import Java. util. extends; public class main {/*** @ Param ARGs */public static void main (string [] ARGs) {int A; using CIN = new using (New bufferedinputstream (system. in); While (CIN. hasnext () {A = cin. nextint (); system. out. println (isodd (a);}/*** determine whether a number is an odd number ** @ Param N * @ return */public static Boolean isodd (int n) {return n % 2 = 1 ;}}/* 2-2 3-3falsefalsetruefalse */

Use n % 2! = 0 to determine whether the integer is an odd number:

Import Java. io. bufferedinputstream; import Java. util. extends; public class main {/*** @ Param ARGs */public static void main (string [] ARGs) {int A; using CIN = new using (New bufferedinputstream (system. in); While (CIN. hasnext () {A = cin. nextint (); system. out. println (isodd (a);}/*** determine whether a number is an odd number ** @ Param N * @ return */public static Boolean isodd (int n) {return n % 2! = 0;}/** 2-2 3-3 False false true */

Use N & 1 = 1 to determine whether the integer is an odd number:

Import Java. io. bufferedinputstream; import Java. util. extends; public class main {/*** @ Param ARGs */public static void main (string [] ARGs) {int A; using CIN = new using (New bufferedinputstream (system. in); While (CIN. hasnext () {A = cin. nextint (); system. out. println (isodd (a);}/*** determine whether a number is an odd number ** @ Param N * @ return */public static Boolean isodd (int n) {return (N & 1) = 1 ;}/ ** 2-2 3-3 False false true */

To sum up, determine whether an integer is an odd number and use N % 2! = 0 and N & 1 = 1 are correct.

However, if an integer is an even number, it is correct to use N % 2 = 0 and N & 1 = 0.

Incorrect syntax:

Odd: N % 2 = 1

Even number: N % 2! = 1

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.