1. ## Problem in Loop!!

n numbers are entered from the keyboard into an array. Write a program to find out many of them are positive, how many are negative, how many are even and how many odd.

Sample Input #Supposed to be)
Enter 10 numbers : 10 20 23 -6 52 34 22 66 -22 -1
Sample Output #1:
Positive Numbers : 7
Negative Numbers : 3
Odd Numbers : 2
Even Numbers : 8

Code:
```#include <stdio.h>
#define MAX_INP 10

int main ()
{
int no[MAX_INP]={0},i=0,j=1,pos=0,neg=0,odd=0,even=0;

for (i =0; i<10; i++){
printf("Enter no %d: ",j);
scanf("%d",&no[i]);
j++;
}

for (i=0; i<10; i++){
if(no[i] > 0){
pos++;
}

if(no[i] < 0){
neg++;
}

if((no[i]/2) == 1 || no[i]/2 == -1){
odd++;
}

if((no[i]/2) == 0){
even++;
}
}

printf("\n\nPositive numbers : %d\nNegative Numbers : %d\nOdd Numbers : %d\nEven Numbers : %d",pos,neg,odd,even);

getch ();
return 0;
}```

Out put is not shown correctly in case of odd and even while =ve and -ve numbers are showing properly. Please Help!!!!!

2. Nothing to do with the loop. It is your tests for what is odd or even that are broken.

If no[i] is 23, then no[i]/2 is 10 (integer arithmetic). The tests you are doing will not classify it as odd or even.

If no[i] is -1 then no[i]/2 is zero. Your test means it will be counter it as even.

3. OOOH!!! my mistake. i mistakenly used / instead of %

4. Originally Posted by fredsilvester93
Out put is not shown correctly in case of odd and even while =ve and -ve numbers are showing properly. Please Help!!!!!
The easiy way to detect an odd or even number is by testing bit 0...
Code:
```if (number & 1)
odd++;
else
even++;```

5. Originally Posted by CommonTater
The easiy way to detect an odd or even number is by testing bit 0...
Code:
```if (number & 1)
odd++;
else
even++;```
The slightly less cryptic way is use modulo 2. It requires no more typing, and there is some advantage to understanding through computing a (trivial) numeric attribute using a numerical operation rather than a bitwise operation. No need to know what a bit is, what bit 0 is .....

Bitwise operations do have some implementation-defined or undefined characteristics on signed types too.

6. ## RE:

Originally Posted by CommonTater
The easiy way to detect an odd or even number is by testing bit 0...
Code:
```if (number & 1)
odd++;
else
even++;```
Didn't get the Logic... what does ''if (number & 1) means?''

i used ''if(no%2 == 1)''

help plz///

7. Originally Posted by fredsilvester93
what does ''if (number & 1) means?
You could read up on bitwise operators, but for your purposes what you used works just as well.

8. Originally Posted by fredsilvester93
Didn't get the Logic... what does ''if (number & 1) means?''

i used ''if(no%2 == 1)''

help plz///
It's capitalizing on how integers are stored in C ... an integer is just a binary number.

In the binary numbering system all odd numbers have a 1 at bit position 0.

0 = 00000000
1 = 00000001 <-- odd
2 = 00000010 < -- even
3 = 00000011 <--- odd
and so on...

So if we AND the number with 1 (i.e. bit 0 = 1) we will get a 1 or a 0 indicating odd or even.

01010011
AND
00000001
======
00000001 <-- odd

11011000
AND
00000001
======
00000000 <-- even

Since an if() statement responds to TRUE (1) or FALSE (0) we can easily use the result of number AND 1 to trigger it.

9. ## RE:

Originally Posted by CommonTater
It's capitalizing on how integers are stored in C ... an integer is just a binary number.

In the binary numbering system all odd numbers have a 1 at bit position 0.

0 = 00000000
1 = 00000001 <-- odd
2 = 00000010 < -- even
3 = 00000011 <--- odd
and so on...

So if we AND the number with 1 (i.e. bit 0 = 1) we will get a 1 or a 0 indicating odd or even.

01010011
AND
00000001
======
00000001 <-- odd

11011000
AND
00000001
======
00000000 <-- even

Since an if() statement responds to TRUE (1) or FALSE (0) we can easily use the result of number AND 1 to trigger it.

ok . like if (no & 1 == 0) will do even++ of no is 2????

10. Originally Posted by fredsilvester93
ok . like if (no & 1 == 0) will do even++ of no is 2????
Any even number. That's the whole point... that last bit is 1 for all odd numbers, 0 for all even numbers.
The thing is we have to strip the rest off so we can look at that one bit in isolation, so we use AND.

11. oooooh gr8... got the concept after applying.. thanks alot allllllll........ it a gr8 concept....