mardi 19 juillet 2016

Changing even digits in a number to odd

I am trying to pick the even digits from a number and convert them to odd by adding 1 to it

example input/output

n = 258463, ans = 359573

    int n=26540;
    System.out.println("n= "+n+", ans= "+even2odd(n));
    n=9528;
    System.out.println("n= "+n+", ans= "+even2odd(n));

public static int even2odd(int n)
{

while ( n > 0 ) {
    if (n%2==0) {
        n +=1;
    }
    System.out.print( n % 10);
    n = n / 10;
}
int ans = n;

return ans; 
}

as you can see right I managed to convert all the even digits to odd but i dont know how to reverse them back into order and output it in the right place

Aucun commentaire:

Enregistrer un commentaire