# Thread: How to find an even and odd position in an int/long?

1. Member
Join Date
Oct 2013
Posts
8
Rep Power
0

## How to find an even and odd position in an int/long?

Hello,

For example (long input):

7892472304

4, 3, 7, 2, 8 are in the odd positions, while 0, 2, 4, 9, 7 are in the even positions.

What I'm trying to do is do something to the ones in even position and something else for the ones in the odd position. I have used mod 10 to extract each number, but I am not sure how I can check if something is in even or odd position.

2. Member
Join Date
Oct 2013
Posts
7
Rep Power
0

## Re: How to find an even and odd position in an int/long?

a simmple boolean will let u pick switch between the to the first number will always be odd assumeing your starting from the back then once you do what you want to odd switch your (isodd) boolean to false
ie
while (a <=NUMLENGHT)
{
//cuts and ads the last number on the string to cSum and switches to other equation
if (isEven==false){

cardNum=cardNum/10;
isEven=true;
a++;
}
//preforms the slightly more complicated math as asked for then switchs back to other equation
else{
oTen=(cardNum%10)*2;
if(oTen>=10){
oTen=oTen-9;}
cardNum=cardNum/10;
isEven=false;
a++;
}

3. Member
Join Date
Oct 2013
Posts
7
Rep Power
0

## Re: How to find an even and odd position in an int/long?

i cut part of the code out incase were in the same class cause if i do your home work now youll be fucked for the finals / later assinment

4. Member
Join Date
Oct 2013
Posts
8
Rep Power
0

## Re: How to find an even and odd position in an int/long?

Originally Posted by donohoes
i cut part of the code out incase were in the same class cause if i do your home work now youll be fucked for the finals / later assinment
I kind of thought of a different way to do it, but thanks man.

5. ## Re: How to find an even and odd position in an int/long?

Originally Posted by imanoob
I kind of thought of a different way to do it, but thanks man.
Try it with the numbers 12 and 123 and see if it works ...

kind regards,

Jos

6. Member
Join Date
Oct 2013
Posts
8
Rep Power
0

## Re: How to find an even and odd position in an int/long?

Originally Posted by JosAH
Try it with the numbers 12 and 123 and see if it works ...

kind regards,

Jos
Thanks for the reply. I did it in a different way. What I did was set a condition inside the mod 10 loop, like:

if (count%2 == 0)
//calculations
else
//calculations

count++

So everytime it goes through the loop, the if-condition alternates between every other digit. I hope this helps anyone.