Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,153,462 members, 7,819,688 topics. Date: Monday, 06 May 2024 at 09:01 PM

Checking If A Number Is Odd In Java - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Checking If A Number Is Odd In Java (1284 Views)

Can An Array In Java Hold Multiple Types? / Community Service | I Am To Teach Everything I Know In Java For Free! / I Need Help In Java Programming. (2) (3) (4)

(1) (Reply) (Go Down)

Checking If A Number Is Odd In Java by D34lw4p(m): 10:54pm On Jan 25, 2016
Have a look at the lines of code below and determine if they can be used to precisely identify if a given number is Odd?


public boolean oddOrNot(intnum) {
return num %2==1;
}


These lines seem correct but they will return incorrect results one of every four times (Statistically speaking). Consider a negative Odd number, the remainder of division with 2 will not be 1. So, the returned result will be false which is incorrect!

This can be fixed as follows:


public boolean oddOrNot(intnum) {
return (num &1) !=0;
}


Using this code, not only is the problem of negative odd numbers solved, but this code is also highly optimized.

Since, Arithmetic and Logical operations are much faster compared to division and multiplication, the results are achieved faster so in second snippet.


Join Our Programming Discussion Forum www.nct.com.ng
Re: Checking If A Number Is Odd In Java by Standing5(m): 11:00pm On Jan 25, 2016
If you use a modulus or absolute value to convert negetive No to positive then it will work fine.
Re: Checking If A Number Is Odd In Java by sinequanon: 5:30pm On Jan 26, 2016
What about:

public boolean oddOrNot(intnum) {
return (num |1) ==num;
}

It won't have to load the literal 0;
Re: Checking If A Number Is Odd In Java by Nobody: 1:21pm On Jan 29, 2016
 public static void checkNum(int Num){
 if(num%2 == 0){
 System.out.println("The number" +Num+ "is even"wink;
 }else{
 System.out.println("The number" +Num+ " is odd"wink;
 }
 }
 
Re: Checking If A Number Is Odd In Java by Fulaman198(m): 8:17pm On Jan 29, 2016
GetAvenue:

public static void checkNum(int Num){
if(num%2 == 0){
System.out.println("The number" +Num+ "is even"wink;
}else{
System.out.println("The number" +Num+ " is odd"wink;
}
}

That's how I would have done it personally.

(1) (Reply)

PHP Functions To Clean Database Inputs / Conflict Between Apache24 And Simple Server On My System. PLS HELP!!!!!!!!!!!!!! / See What Paypal Stated About E-commerce Shopping By Nigerians (FEATURES)

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 12
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.