[tricky method] Find whether a number is odd or even.

Status
Not open for further replies.

gary4gar

GaurishSharma.com
I ask you how to Find whether a number is odd or even. you woudl say what a big deal.
its a easy question but the catch is

Conditions :-
  • Not to use Division Operator (/), Multiplication Operator (*) and Modulous Operator (%)/
  • We can use Addition Operator (+) and Substraction Operator (-)


now How it can be done with the above conditions ?
 

ray|raven

Think Zen.
Isnt the best method but works;

Code:
public class TrickyOdd
{
    public static void main(String args[])
    {
        int number; //Number to be determined as odd or even
        
        for(;number>1;number=number-2);
        if(number==1)
            System.out.println("Odd");
        else
            System.out.println("Even");
    }
}

Code's in Java btw.
 
OP
gary4gar

gary4gar

GaurishSharma.com
Thanks for a super quick reply but what language is it?
JAVA?

But yeah i did understand the logic part somewhat.

Here is a C version of it. which i tried to make
Code:
#include<stdio.h>
int main()
{
	int number;
	printf("Please Enter the Number You wish to check\n");
	scanf("%d", &number);
	for(;number>1;number-=2);
	{
		if(number == 1)
 
			printf("Odd number");
		else
			printf("even number");
 
	}
	return 0;
}
But the problem is i get print multiple times.
here what i get when i run it

Please Enter the Number You wish to check
?9
even number
even number
even number
even number
 
Last edited:

ray|raven

Think Zen.
Dude, you forgot the semi-colon at the end of the for.
Here's the C code

Code:
#include<stdio.h>
int main()
{
    int number;
    printf("Please Enter the Number You wish to check\n");
    scanf("%d", &number);
    for(;number>1;number-=2);
        if(number == 1)
            printf("Odd number");
        else
            printf("even number");
 
    }
    return 0;
}
 

mehulved

18 Till I Die............
How about this
Code:
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {
	int num;

	printf("Please Enter a Number > ");
	scanf("%d", &num);

	if((num&1)==1)
		printf("%d is odd", num\n);
	else
		printf("%d is even", num\n);
	
	return 0;
	exit(0);
}
 

QwertyManiac

Commander in Chief
Yep since all odd numbers have their 2^0th bit set (Even + 1 = Odd) you can simply AND it with 1 to find if its even or odd.
 

dheeraj_kumar

Legen-wait for it-dary!
I think you can do something with the shift left and shift right operators << and >>. I did something of that sort at school, now I dont remember.

something like
Code:
while (num > 1)
      num >>= 1;
if (num == 1)
      print "odd"
else
      print "even"
 

dheeraj_kumar

Legen-wait for it-dary!
Hmm... I remember doing this with >> very well. and yeah, all it does is divide by 2. The topic started asked us not to use / operator so I did like this.

while (number > 1)
divide by 2 continuously. using shift right.

when the loop stops, it means the number now should be 1 or 0.
1 means there is a remainder. even.
0 means there isnt.odd.

perhaps my coding was wrong, but the logic sounds ok, doesnt it?
 

victor_rambo

हॉर्न ओके प्लीज़
Ok here is my logic:
1. Get the input
2. Get the last digit of the input
3. Compare if last digit is any of these: 0, 2, 4, 6 and 8.
 

QwertyManiac

Commander in Chief
Very wrong. As you see, the division by 2 is an Integer division, so all that you get in the end would be == 1.

Besides, how are you gonna find the remainder without having to use the divmod or mod operations. Could you show it in some implementation?

Using your logic for 'variable' remainders, if thats what you meant, here's the try out:

Code:
>>> def div(x):
...     while x>1:
...             x>>=1
...     return x
... 
>>> for x in range(1,50): 
...     print div(x)
... 
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1

You can see it, all kinds of numbers will only return 1.

Ps. Good try Rohan, but I bet its efficiency is very low. Let's check:

Code:
#!/usr/bin/env python

import time

def str_method(x):
	x=str(x)[-1]
	s=set('02468')
	if x in s:
		return 0
	else:
		return 1

def bin_method(x):
	if x & 1:
		return 1
	else:
		return 0

m=time.time()
map(bin_method,xrange(0,1000))
print time.time()-m

m=time.time()
map(str_method,xrange(0,1000))
print time.time()-m

"""
Tries:
Q@Q ~ $ python test.py
0.000995874404907
0.0157248973846
Q@Q ~ $ python test.py
0.000957012176514
0.0170290470123
"""
 
Last edited:

victor_rambo

हॉर्न ओके प्लीज़
How will we get the last digit without using modulus?
I don't think that should be difficult. In PHP, there is a function for this. Since PHP syntax is derived from C, I am sure there must be a similar function in C too.

Ok, here is the PHP code to do it. As I said, there is an inbuilt function to get a substring out from a string.
PHP:
<?php
$input="12345";//input number
$last_digit=substr($input,"-1");//get the last digit
if($last_digit=="0" || 
$last_digit=="2" || 
$last_digit=="4" || 
$last_digit=="6" || 
$last_digit=="8")
//compare the last digit to see if its either of 0, 2, 4, 6 or 8, or use in_array() function.
{
print "Input is an even number";
}
else
{
print "Input is an odd number";
}
?>
 
Last edited:
Status
Not open for further replies.
Top Bottom