number in reverse order

Page 1 of 1

11 Replies - 19940 Views - Last Post: 08 August 2009 - 04:54 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=114164&amp;s=c4124d594ae1ba781a4a5886b71e4bc8&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 noony

• New D.I.C Head

Reputation: 0
• Posts: 10
• Joined: 10-July 09

number in reverse order

Posted 10 July 2009 - 11:22 PM

hello every one .. please am trying to do this program.. it works for the first 3 digits but not the last one

can u have a look and give me some advice ?? thaxx alot all ;**

```
import java.util.Scanner;
public class ch1 {
public static void main(String []args) {

Scanner input=new Scanner(System.in);

System.out.println("enter number with 4 digits:" );

int a=input.nextInt();

int x,y,z,e;
x= a/1000;// first digit began from the left side
y=(a-(x*1000))/100;// second digit
z=(a-((y*100)+x))/10; // theird digit
e=(a-(x*1000)-(y*100)-(z*10));// fourth digit ///// here is my problem !!!!!

System.out.println(e+""+z+""+y+""+x);

}

}

```

i also try to do it this way .. but it didnt work

```e=((a-(x*1000)-(y*100)-(z*10))/10);// fourth digit

```

Is This A Good Question/Topic? 0

Replies To: number in reverse order

#2 Fuzzyness

• Comp Sci Student

Reputation: 669
• Posts: 2,438
• Joined: 06-March 09

Re: number in reverse order

Posted 10 July 2009 - 11:49 PM

I dont like to make it so that only a certain amount of numbers will work so I did mine to work with all numbers. Takes a number and mathmatically like you did it prints it backwards.

First off, if you do 1234/10 in Java, it will = 123. because thats how many times 10 will go into 1234.
So, if you then times that number by 10, you have that number with a 0 in the 1's position right? then you subtract that from the main number.

You will need a variable num to hold the number they enter, then with that you will do the actions I told you to.

1. Get its divisibility by 10
2. Multiply that by 10, so you know have gotten rid of the 1's place value
3. Subtract the original number from the new number to get what was in the 1's spot to begin with.

num-((num/10)*10)
1. num/10 - Step 1
2. (num/10)*10 - Step 2
3. num- ((num/10)*10) - Step 3

To print it just use a System.out.print and it will print, after you print it though, add a space to it.

Now you can put this inside a loop so it goes through the entire number, what is the stopping point though? When the number no longer has a place value greater then the 1's. so while num/10 > 0

That will give you all the numbers except for the 1's value position of the number. So simply make a println statement to finish off the line so that it prints the remainder of the number, will no longer use num/10 will now usenum%10 That will give the remainder.

Here is an example of all numbers.

```Scanner in = new Scanner(System.in);
System.out.print("Please enter number: ");
int num = in.nextInt();
do{
System.out.print(num-((num/10)*10) + " ");
num /= 10;
}while(num/10 > 0);
System.out.println(num%10);
```

Hope this helps!

This post has been edited by Fuzzyness: 10 July 2009 - 11:50 PM

#3 noony

• New D.I.C Head

Reputation: 0
• Posts: 10
• Joined: 10-July 09

Re: number in reverse order

Posted 11 July 2009 - 12:10 AM

thanx alot for your help fuzzyness it waaaaas reeeeaaaaaaaaaaaaaaaly helpful ...

#4 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12276
• Posts: 45,364
• Joined: 27-December 08

Re: number in reverse order

Posted 11 July 2009 - 08:07 AM

Why don't you read in the number as a String and simply do a for loop from the length()-1 to 0. Let me show you:

```Scanner scan = new Scanner(System.in);
String s = scan.next();
for(int i = s.length()-1; i > 0; i--)
System.out.print(s.charAt(i));

```

Hope this helps!

#5 Fuzzyness

• Comp Sci Student

Reputation: 669
• Posts: 2,438
• Joined: 06-March 09

Re: number in reverse order

Posted 11 July 2009 - 02:55 PM

macosxnerd101, on 11 Jul, 2009 - 07:07 AM, said:

Why don't you read in the number as a String and simply do a for loop from the length()-1 to 0. Let me show you:

```Scanner scan = new Scanner(System.in);
String s = scan.next();
for(int i = s.length()-1; i > 0; i--)
System.out.print(s.charAt(i));

```

Hope this helps!

Yes I would agree that to be easier, I however looked at his class name of ch1 and decided this is probably a homework assignment or a classwork exercise and should stick with the mathmatical way of doing it.. but yes tat would work just as well- Goodjob!

#6 syfran

• D.I.C Lover

Reputation: 83
• Posts: 1,103
• Joined: 12-July 09

Re: number in reverse order

Posted 12 July 2009 - 04:29 AM

All math and works with different lengths.

```int length = (int) Math.ceil(Math.log10(toReverse));

for(int pos = 1; pos <= length; pos++) {
reversed *= 10;
reversed += toReverse%10;
toReverse= (int) Math.floor(toReverse/10);

}

```

This post has been edited by syfran: 12 July 2009 - 04:31 AM

#7 Riddic

• New D.I.C Head

Reputation: 0
• Posts: 1
• Joined: 13-July 09

Re: number in reverse order

Posted 13 July 2009 - 02:09 PM

noony, on 10 Jul, 2009 - 10:22 PM, said:

hello every one .. please am trying to do this program.. it works for the first 3 digits but not the last one

can u have a look and give me some advice ?? thaxx alot all ;**

```
import java.util.Scanner;
public class ch1 {
public static void main(String []args) {

Scanner input=new Scanner(System.in);

System.out.println("enter number with 4 digits:" );

int a=input.nextInt();

int x,y,z,e;
x= a/1000;// first digit began from the left side
y=(a-(x*1000))/100;// second digit
z=(a-((y*100)+x))/10; // theird digit
e=(a-(x*1000)-(y*100)-(z*10));// fourth digit ///// here is my problem !!!!!

System.out.println(e+""+z+""+y+""+x);

}

}

```

i also try to do it this way .. but it didnt work

```e=((a-(x*1000)-(y*100)-(z*10))/10);// fourth digit

```

Hey if u don't have to stick to the mathematical way, the easiest way i can think of is changing your input to StringBuffer. StringBuffer has the .reverse() method that automatically changes anything to reverse order.

```   import java.util.*;
public class ch1 {
public static void main(String []args) {

Scanner input=new Scanner(System.in);

System.out.println("enter number with 4 digits:" );

int a=input.nextInt();
String stringOFa = Integer.toString(a);
StringBuffer buffer = new StringBuffer(stringOFa);
buffer.reverse();
stringOFa = buffer.toString();
a = Integer.parseInt(stringOFa);
System.out.println(buffer);
}
}

```

Here is the I/O for it:
enter number with 4 digits:
126235
532621

(If u need the result as an int then just ignore my last line of code.)

This post has been edited by Riddic: 13 July 2009 - 02:13 PM

#8 noony

• New D.I.C Head

Reputation: 0
• Posts: 10
• Joined: 10-July 09

Re: number in reverse order

Posted 08 August 2009 - 06:28 AM

thanx alot every one.. yes reading it as a string is much easier to remember ..
befor few weeks i was unable to think this way hehee ! it was the beging of my first java course .... am now much better .. thanx for all of u who tried to help me

Fuzzyness, on 11 Jul, 2009 - 01:55 PM, said:

macosxnerd101, on 11 Jul, 2009 - 07:07 AM, said:

Why don't you read in the number as a String and simply do a for loop from the length()-1 to 0. Let me show you:

```Scanner scan = new Scanner(System.in);
String s = scan.next();
for(int i = s.length()-1; i > 0; i--)
System.out.print(s.charAt(i));

```

Hope this helps!

Yes I would agree that to be easier, I however looked at his class name of ch1 and decided this is probably a homework assignment or a classwork exercise and should stick with the mathmatical way of doing it.. but yes tat would work just as well- Goodjob!

yes you are right .. this is the best way to do it and it works for any number of digits (Y) thank you

#9 baavgai

• Dreaming Coder

Reputation: 7151
• Posts: 14,894
• Joined: 16-October 07

Re: number in reverse order

Posted 08 August 2009 - 07:25 AM

A lot of source code here. I'll throw in mine.

I'd do it like this:
```void printReverse(int n) {
if (n==0) { return; }
System.out.print(n%10);
printReverse(n/10);
}

```

#10 virgul

• D.I.C Regular

Reputation: 44
• Posts: 269
• Joined: 18-March 09

Re: number in reverse order

Posted 08 August 2009 - 01:56 PM

baavgai is it your job to do the recursive method at your work or something? Noony, be sure to keep his code handy for when you start the chapters in recursion. I must say that he is by far one of the best at it

#11 baavgai

• Dreaming Coder

Reputation: 7151
• Posts: 14,894
• Joined: 16-October 07

Re: number in reverse order

Posted 08 August 2009 - 02:16 PM

virgul, on 8 Aug, 2009 - 02:56 PM, said:

baavgai is it your job to do the recursive method at your work or something?

Lol, other than tree walking and "divide and conquer" type stuff, recursion doesn't come up all the much in the real world. Well, unless you're a functional programmer. So, when you can actuall apply it, it's kind of fun.

The interative version, which is just as simple, would look like this:
```void printReverse(int n) {
while (n!=0) {
System.out.print(n%10);
n /= 10;
}
}

```

#12 mostyfriedman

• The Algorithmi

Reputation: 729
• Posts: 4,473
• Joined: 24-October 08

Re: number in reverse order

Posted 08 August 2009 - 04:54 PM

recursion kicks ass...its much more fun than iteration