number in reverse order

printing intger with 4-digits in reverse order

Page 1 of 1

11 Replies - 19389 Views - Last Post: 08 August 2009 - 04:54 PM Rate Topic: -----

#1 noony  Icon User is offline

  • New D.I.C Head

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

number in reverse order

Post icon  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  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • 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

Was This Post Helpful? 1
  • +
  • -

#3 noony  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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 ...
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12135
  • View blog
  • Posts: 45,120
  • 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!
Was This Post Helpful? 1
  • +
  • -

#5 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

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

Re: number in reverse order

Posted 11 July 2009 - 02:55 PM

View Postmacosxnerd101, 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! :^:
Was This Post Helpful? 1
  • +
  • -

#6 syfran  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#7 Riddic  Icon User is offline

  • New D.I.C Head

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

Re: number in reverse order

Posted 13 July 2009 - 02:09 PM

View Postnoony, 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

Was This Post Helpful? 0
  • +
  • -

#8 noony  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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 :)

View PostFuzzyness, on 11 Jul, 2009 - 01:55 PM, said:

View Postmacosxnerd101, 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
Was This Post Helpful? 0
  • +
  • -

#9 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 6966
  • View blog
  • Posts: 14,572
  • 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);
}


Was This Post Helpful? 0
  • +
  • -

#10 virgul  Icon User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#11 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 6966
  • View blog
  • Posts: 14,572
  • Joined: 16-October 07

Re: number in reverse order

Posted 08 August 2009 - 02:16 PM

View Postvirgul, 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;
	}
}


Was This Post Helpful? 0
  • +
  • -

#12 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 729
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1