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.
it should be obvious that it is not necessary to reverse the digits.
You could read the value in as a String and use charAt method.
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.
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.......
> 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);
you dont have to reverse the digits
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);
}
}
}
Couldn't resist could you?
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);
}
}
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);
}
}
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;
}
}
> > 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?
>NO NEED TO SHOUT. WE ARE NOT DEAF!
FEH. THIS IS SHOUTING, LADDIE!!!!!!!!!!!!!
> I don't get one part of this code. What is the "odd" variable for?
its used to toggle between add and subtract.
> I don't get one part of this code. What is the "odd" variable for?
its used to toggle between add and subtract.
int getReverse(int i) {
int j = 0;
while (i != 0) {
j *= 10;
j += i % 10;
i /= 10;
}
return j;
}
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/