English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

find the number is even or not withought using % or / operator

2006-10-11 06:50:40 · 7 answers · asked by Anonymous in Computers & Internet Programming & Design

7 answers

Hi Gulraize,

If we treat this as a math problem rather than a C language problem then it means whether the number is a multiple of 2 or not. So
1. Subtract 2 from the number and check the result.
2. If the result is (1 or less than 0) then number is odd, else If the result is 0 then the number is even, else subtract 2 from the result again and keep on checking (i.e. executing step 2) until you find the answer.

No % or / used in the above example.

Now if we treat this as computer specific problem, then
odd number = even number + 1.
This simply means check whether the LSB of the number is set or not. (i.e. if((number & 1) == 1)) If any bit other than the LSB bit is set then it would add only even value to the number. You can only add 1 by setting the LSB bit.

Now create the equivalent C program yourself.

2006-10-11 08:10:47 · answer #1 · answered by Achint Mehta 3 · 0 0

Do a bitwise AND (the & operator) with 00000000001 (sorry, put in the right # of 0's) and compare the result to 1. If the number = 1, then the last bit in the number was a 1, so the number was odd; if the number = 0, then the last bit in the number was a 0, so the number was even.

In fact, since zero = false and one = nonzero = true, that gives you a logical test on the result of the & operation.

2006-10-11 07:02:39 · answer #2 · answered by Jay H 5 · 2 0

What horrible coding style the other answers have. Will H has needless multiple return paths, all of which return a hard-coded number instead of a symbolic constant, and uses ints instead of bools. Then the other guy has main returning void on top of that. Use this instead. bool IsPrime(unsigned int num) { bool prime=false; if(num>1) { prime=true; for(unsigned int i=2;i<=sqrt(num);++i) { if(!(num%i)) { prime=false; break; } } } return prime; } NOTE: sqrt() is a math function that takes floating point values, so you may want to do some casting or use a different square root function.

2016-03-28 05:07:45 · answer #3 · answered by Anonymous · 0 0

I don't know the exact code but divide the number and test to see if it is an integer or not. If it has decimals then it is not an integer.

2006-10-11 06:53:45 · answer #4 · answered by Anonymous · 0 2

u can't do it without the% operator
if u want the code with the%operator, here it is
mian()
{
int n;
scanf("%d",n);
if((n%2)==0)
printf("the numbet is even);
else
printf("the number is odd or zero);
}

2006-10-11 07:02:01 · answer #5 · answered by Anonymous · 0 2

not sure about c but
its something like
if int(x/2) =(x/2) then its even
if not int(x/2)=(x/2) then its odd.

2006-10-11 06:59:48 · answer #6 · answered by papeche 5 · 0 2

divide by two, use %
if the remainder is zero, the orig # is even
if the remainder is one, it's odd.

2006-10-11 06:58:33 · answer #7 · answered by Sufi 7 · 1 2

fedest.com, questions and answers