This is been answered with `modulo`

operator here Java, Check if integer is multiple of a number

I wanted to know if there is a way without using `division`

or `modulo`

operator.

This is an part of interview question I am trying to solve online for fun

Recall that binary representation of even numbers has the least significant bit set to zero. Now the solution is easy: mask the last bit with `1`

using bitwise AND, and compare the result to zero. If you get a zero, the number is even; otherwise, it is odd.

```
boolean isEven(int a) {
return (a & 1) == 0;
}
```

When you do `a & 1`

, you get the result that is either 1 or zero, depending on the value of the last bit. The jargon for this is "bit masking": in this case, `1`

is used as a bit mask.

int i = 5; if ((i & 0x1) == 0) { System.out.println("It's a multiple of 2"); } else { System.out.println("It's NOT a multiple of 2"); }

You can also use shift bit, shift one bit to the right and shift back, which also equivalent to divide by 2 and multiply 2 back

```
boolean isEven(int val){
if(((val>>1)<<1) == val){
return true;
}
}
```

One of simple method would be like,

```
int no=5;
String str=""+no;
if((str.length()-1).equals("2")||(str.length()-1).equals("4")||(str.length()-1).equals("6")||(str.length()-1).equals("8")||(str.length()-1).equals("0")||)
System.out.println("Yes");
System.out.println("No");
```

As simple as that. Hope it helps.

Similar Questions

I have two in the following subquery inner select. I would like to division(subquery1/subquery2) two subquery. How do you do it? Select subquery1/subquery2,subquery1,subquery2 From..... Thanks...

Questions arise when I type in these expressions to Python 3.3.0 -10 // 3 # -4 -10 % 3 # 2 10 // -3 # -4 10 % -3 # -2 -10 // -3 # 3 It appears as though it takes the approximate floating point (-3.33

I would like to replace all division signs and multiplication signs in a string with the characters / and *. How can I do that? I already tried it with this method but it does not work. public static

I'm trying to add a basic constraint to my solver foundation by doing the following: model.AddConstraint(c1, x % y == 0); I get a compile error saying Operator '%' cannot be applied to operands of

i am beginner in cakephp , and i want use SQL IN operator in find method , i have words table. my code is : $this->Word->find('Word.wordid in (83,82)'); , and this code create this query : SE

I tried to get the result of -1 modulo 1000000007 using the % operator of C++ and fmod function. The output is -1, but -1 modulo 1000000007==1000000006. What have I done wrong?

I have this simple code: [n/2 for n in range(100, 200)] But, strangely enough, it returns [50,50,51,51,52,52,etc], so it uses integer division instead of normal one (I need it to output [50,50.5,51,51

How to model the modulo operator in alloy? I want to try alloy to prove that any multiple of 4 is divisible by 2.... Here's my code.. //proof that 4n is divisible by 2 module I4nDivisibleby2 sig num

Assuming you have a machine instruction udive that does a special case 64 by 32 unsigned division by taking a (32bit dividend << 32) / 32bit divisor, we can do a full 64 by 32 division using the

This question already has an answer here: Java Integer division: How do you produce a double? 4 answers So i am having a difficult time trying to figure out how to get the values to come out pr

I am using below command but i dont know where the WSDL file s created java2ws -o Customer.wsdl -d C:\OMG -t http://customeroffer.manage.ws.usddomega.alu.com -cp C:\omg\classes com.alu.usddomega.ws.ma

Here is the function that finds the greater of two numbers: int larger(int a,int b) { int c=a-b; int k=c>>31&1; int max=a-k*c; return max; } To find the greatest of three numbers, call it s

How to apply multiple operands to a single operator? An example: instead of if (foo == dog || foo == cat || foo == human) I can have the following or similar: if (foo == (dog || cat || hum

This question already has an answer here: What does % do in javascript? 4 answers How can I use modulo operator (%) in calculation of numbers for javascript projects?

Suppose I have a String, say one two three one one two one. Now I'm using a Pattern and a Matcher to find any specific Pattern in the String. Like this: Pattern findMyPattern = Pattern.compile(one);

I've made a calculator with java using JFrame, and everything works except the division. Here is the bit of code which calculates the answer and displays it: public void actionPerformed(ActionEvent e)

There is integer variable, voltage in millivolts. signed int voltage_mv = 134; //134mV I have 2-segment display and I want to display hundredths of volts. How can I convert milivolts to hundredths vo

The // integer division operator of Python surprised me, today: >>> math.floor(11/1.1) 10.0 >>> 11//1.1 9.0 The documentation reads (floored) quotient of x and y. So, why is mat

I wanted to check the availability of multiple strings in a given string ( without using a loop ). like my_string = How to find occurence of multiple sting in a given string using javascript RegExp;

I need to find the factorial in java without using loop or recursion ? So if there is any way then please help . Thanks

I am confused by NSDecimalNumber and its behaviors. I have an NSDecimalNumber that represents a dollar value, say $37.50. I'd like to find out how many times say 5.0 goes into that number and then k

I have to find the value of ( 1+sqrt(3) )^n where n < 10^9.As this number can be very large we have to print the ans%1000000007. I have written the following function for this. double power(double

Is there a way to convert a byte array to string other than using new String(bytearray)? The exact problem is I transmit a json-formatted string over the network through UDP connection. At the other e

I am new to Groovy. why this throws exception on runtime: int[] a = [1,2,3,4,5] int lo=0 int hi=4 int x = a[(lo+hi)/2] assert x == 3 while these are ok: int x = a[(int)(lo+hi)/2] and int i = (lo+hi)

For example, the standard division symbol '/' rounds to zero: >>> 4 / 100 0 However, I want it to return 0.04. What do I use?

I am struggling to come up with the SQL query to find the all the products accessible to user under a particular division. A division can have many products. Division table has division_id and divisi

In Wikipedia it states that a modulo operation is required to check the available space in a circular buffer. However in my implementation I simply do: static size_t bytes_used(const ringbuffer* rb) {

I am doing very simple int division and I am getting odd results. This code prints 2 as expected: public static void main(String[] args) { int i = 200; int hundNum = i / 100; System.out.println(hundNu

Can some one guide in implementing the relational algebra division operator using sorting with syntactic support in postgresql? Edit: This is to deal with the source code of postgresql. I need to add

how to find out the nearest place using Java (is there any third party lib which provides this support) some thing like this my requirement is : Enter the zip code or place name select the categories

How do I add two numbers without using ++ or + or any other arithmetic operator? It was a question asked a long time ago in some campus interview. Anyway, today someone asked a question regarding some

I have the following: int count = args.length; Strange as it might seem I want to find out the array length without using the length field. Is there any other way? Here's what I already (without succ

I remember in java that, the modulo operator could be inverted so that rather than seeing what the remainder is of an operation, you could invert it, so instead it will tell you many times a number wa

Is there something like a modulo-operator in x86 Assembler?

Possible Duplicate: Integer wrapper class and == operator - where is behavior specified? I known Java integer use cache in -127~128. If Integer i = 1; Integer j = 1; Integer m = 128; Integer n = 12

I'm doing some training exercises, but I just can't seem to understand a particular assignment: Write a method that imitates the Java integer division / for doubles and returns an int type value. Ho

How to set the classpath using a java program without setting it form command line?

Does anyone know why integer division in c# returns an integer but not a float? What is the idea behind (is it only a legacy of C/C++)? In C#: float x = 13 / 4; //imagine I used have overridden == ope

I have a scenario like, connecting multiple dbuser nothing but schema at a time to a same database using java code, the tables from 2 schema i have to join using Java,please let me know how to do. For

Possible Duplicate: How does XOR variable swapping work? Swap the values of two variables without using third variable How can I write a C code to swap 2 integer variables without using any extra va

I need to find the modulo for float numbers. For that, I am using NSLog(@value >> %f,fmodf(2.0f, 0.1f)); The output for this should be 0.0f But I am getting the output value >> 0.1 How?

I need to find the Last td of every row in a HTML table, and remove a css from that td. Can I do it without using Jquery. How?

What does modulo in the following piece of code do ? from math import * 3.14 % 2 * pi How do we calculate module on a floating point number ?

Given a table called namespace_list with an INTEGER[] column called namespace_ids. I have the following query: query = SELECT * FROM namespace_list WHERE namespace_ids = ANY(%s) and application_id=%s

The requirements of this are somewhat restrictive because of the machinery this will eventually be implemented on (a GPU). I have an unsigned integer, and I am trying to extract each individual digit.

I came across a interview question which reads as follows: Write a simple C/C++ Macro to find maximum of two numbers without using std library or ternary operator. I need your help in solving this.

Does the JavaScript code var n = 8; // or some arbitrary integer literal n >> 1; always denote integer devision by 2 without remainer? My concern is the endianess if the integer literal is la

Using Java, is it possible to detect user actions, such as key-presses, mouse-button-presses, and/or mouse's screen location, without the use of GUI elements? If it is, how could I implement it? Other

I'm tweaking an XStream output, and I get the following: <entry> <string>ahh</string> <java-class>java.lang.Integer</java-class> </entry> So I try to create an al

I understand this: 35%10 returns 5 but, why does 000000035%10 return 9? Ruby does the same thing. I checked with irb. Should I strip the 0s that are padding the number? What is the java function to do