Reversing an integer.....

how do i solve this riddle?

I'll input an integer (doesn't matter how long) and then reverse this integer to perform this:

suppose: n = 8784204

reverse n and extract each digit to perform this calculation:

4-0+2-4+8-7+8 = 11

this 11 would be the output.

/: I'm having a problem on how the addition and subtraction will work with each digit, specially if it doesnt matter how long the number will be.

[446 byte] By [Java_indigestiona] at [2007-11-27 11:29:30]
# 1

it should be obvious that it is not necessary to reverse the digits.

TuringPesta at 2007-7-29 16:28:20 > top of Java-index,Java Essentials,New To Java...
# 2

You could read the value in as a String and use charAt method.

floundera at 2007-7-29 16:28:20 > top of Java-index,Java Essentials,New To Java...
# 3

well that just how it goes.

The user will input an integer.

The algorithm would reverse it,

Subtract and add each digit.(n1,n2, n3,n4,......)

kinda like this

output = n1- n2+n3-n4+n5-n6.............

Output the sum.

Java_indigestiona at 2007-7-29 16:28:20 > top of Java-index,Java Essentials,New To Java...
# 4

I ALMOST FORGOT....

THE PROBLEM SAYS: NO STRING OR ARRAYS USED. JUST THE BASICS.

THERE IS ALSO THIS HINT: USE THE % (MODULO ) TO EXTRACT EACH DIGIT.

Its really confusing.......

Java_indigestiona at 2007-7-29 16:28:20 > top of Java-index,Java Essentials,New To Java...
# 5

no strings?

lrngjavaa at 2007-7-29 16:28:20 > top of Java-index,Java Essentials,New To Java...
# 6

> I ALMOST FORGOT....

>

>

> THE PROBLEM SAYS: NO STRING OR ARRAYS USED. JUST THE

> BASICS.

>

> THERE IS ALSO THIS HINT: USE THE % (MODULO ) TO

> EXTRACT EACH DIGIT.

>

> Its really confusing.......

NO NEED TO SHOUT. WE ARE NOT DEAF!

int number = 1234;

System.out.println(number % 10);

System.out.println(number / 10);

floundera at 2007-7-29 16:28:20 > top of Java-index,Java Essentials,New To Java...
# 7

you dont have to reverse the digits

TuringPesta at 2007-7-29 16:28:20 > top of Java-index,Java Essentials,New To Java...
# 8

public class IntSplitter{

public static void main(String[] args){

int number = 1234;

int result = 0;

boolean odd = true;

while(number > 0){

int digit = number % 10;

number /= 10;

result += (odd = !odd) ? -digit : digit;

System.out.println("Number: " + number + " , Digit: " + digit + " , Result: " + result);

}

}

}

TuringPesta at 2007-7-29 16:28:20 > top of Java-index,Java Essentials,New To Java...
# 9

Couldn't resist could you?

floundera at 2007-7-29 16:28:20 > top of Java-index,Java Essentials,New To Java...
# 10

hi, have a nice day...!

public class Reverse {

public static void main(String args[]){

int x=8784204;

int digit = 0 , sign =1,ans =0;

int bal = x;

while (bal > 0){

digit = bal % 10;

bal /=10;

ans += (digit * sign);

sign *= -1;

}

System.out.println("Answer:" + ans);

}

}

jeyrama at 2007-7-29 16:28:20 > top of Java-index,Java Essentials,New To Java...
# 11

hi! .. have a nice day....!

public class Reverse {

public static void main(String args[]){

int x=8784204;

int digit = 0 , sign =1,ans =0;

int bal = x;

while (bal > 0){

digit = bal % 10;

bal /=10;

ans += (digit * sign);

sign *= -1;

}

System.out.println("Answer:" + ans);

}

}

jeyrama at 2007-7-29 16:28:20 > top of Java-index,Java Essentials,New To Java...
# 12

Try this

public class ReverseInt

{

public static void main(String[] args)

{

int input=97603;

boolean done=false;

int numDigits=getDigits(input);

double result=0;

while(!done)

{

numDigits--;

int digit=input % 10;

if(digit!=0)

result+=Math.pow(10,numDigits)*digit;

input/= 10;

if(input<1)

done=true;

}

System.out.println(result);

}

static int getDigits(int i)

{

int count=0;

int temp=i;

do{

count++;

temp=temp/10;

}while(temp>1);

return count;

}

}

shagila at 2007-7-29 16:28:20 > top of Java-index,Java Essentials,New To Java...
# 13

> > public class IntSplitter{

>

> public static void main(String[] args){

>

> int number = 1234;

> int result = 0;

> boolean odd = true;

>

> while(number > 0){

>

> int digit = number % 10;

> number /= 10;

> result += (odd = !odd) ? -digit : digit;

>

> System.out.println("Number: " + number + " , Digit:

> : " + digit + " , Result: " + result);

>

> }

>

> }

>

> }

>

I don't get one part of this code. What is the "odd" variable for?

Jahvaha at 2007-7-29 16:28:20 > top of Java-index,Java Essentials,New To Java...
# 14

>NO NEED TO SHOUT. WE ARE NOT DEAF!

FEH. THIS IS SHOUTING, LADDIE!!!!!!!!!!!!!

BigDaddyLoveHandlesa at 2007-7-29 16:28:20 > top of Java-index,Java Essentials,New To Java...
# 15

> I don't get one part of this code. What is the "odd" variable for?

its used to toggle between add and subtract.

TuringPesta at 2007-7-29 16:28:24 > top of Java-index,Java Essentials,New To Java...
# 16

> I don't get one part of this code. What is the "odd" variable for?

its used to toggle between add and subtract.

TuringPesta at 2007-7-29 16:28:24 > top of Java-index,Java Essentials,New To Java...
# 17

int getReverse(int i) {

int j = 0;

while (i != 0) {

j *= 10;

j += i % 10;

i /= 10;

}

return j;

}

mkoryaka at 2007-7-29 16:28:24 > top of Java-index,Java Essentials,New To Java...
# 18

Thanks,I see now. I never read the original post properly i thought he just wanted to add the numbers, didn't notice the subtraction signs/

Jahvaha at 2007-7-29 16:28:24 > top of Java-index,Java Essentials,New To Java...