Results 1 to 6 of 6
  1. #1
    imanoob is offline Member
    Join Date
    Oct 2013
    Posts
    8
    Rep Power
    0

    Default 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.

  2. #2
    donohoes is offline Member
    Join Date
    Oct 2013
    Posts
    7
    Rep Power
    0

    Default 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. #3
    donohoes is offline Member
    Join Date
    Oct 2013
    Posts
    7
    Rep Power
    0

    Default 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. #4
    imanoob is offline Member
    Join Date
    Oct 2013
    Posts
    8
    Rep Power
    0

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

    Quote Originally Posted by donohoes View Post
    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. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,368
    Blog Entries
    7
    Rep Power
    20

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

    Quote Originally Posted by imanoob View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    imanoob is offline Member
    Join Date
    Oct 2013
    Posts
    8
    Rep Power
    0

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

    Quote Originally Posted by JosAH View Post
    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.

Similar Threads

  1. Replies: 4
    Last Post: 03-31-2011, 01:29 PM
  2. Find index position of every word in a String
    By pentace in forum New To Java
    Replies: 6
    Last Post: 06-28-2009, 08:26 PM
  3. get position in string from caret position
    By helloworld111 in forum AWT / Swing
    Replies: 5
    Last Post: 02-19-2009, 01:36 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •