Results 1 to 6 of 6
- 10-19-2013, 01:34 AM #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.
Thank you for your time.
- 10-19-2013, 03:47 AM #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++;
}
- 10-19-2013, 03:48 AM #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
- 10-19-2013, 06:35 PM #4
Member
- Join Date
- Oct 2013
- Posts
- 8
- Rep Power
- 0
- 10-19-2013, 06:59 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 10-20-2013, 06:00 AM #6
Member
- Join Date
- Oct 2013
- Posts
- 8
- Rep Power
- 0
Re: How to find an even and odd position in an int/long?
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.
Similar Threads
-
[find out the lucky position to survive]-will you help me please !
By Digital.Oct in forum New To JavaReplies: 4Last Post: 03-31-2011, 02:29 PM -
Find index position of every word in a String
By pentace in forum New To JavaReplies: 6Last Post: 06-28-2009, 09:26 PM -
get position in string from caret position
By helloworld111 in forum AWT / SwingReplies: 5Last Post: 02-19-2009, 02:36 AM
Bookmarks