• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

reverse a number ends with 0

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there

I was trying to do a very simple number reverse program in Java,  example 123450 and result should be 054321 but my code returns 54321. If number end with 0 my code doesn't work.
I tried to find solution but most of the solutions used the numbers which doesn't end with 0.

public class DescendingOrder {

       public static  int sortDesc(final int num){
           int reversed_number =0;
           int number = num;
           while(number!=0) {
               int digit = number % 10;
               reversed_number = reversed_number *10 + (digit);
               number = number / 10;
           }
           return reversed_number;

       }

   public static void main(String[] args) {
       System.out.println(DescendingOrder.sortDesc(123450));
   }
}

Can someone please help.

Thanks.
 
Rancher
Posts: 1965
15
Android Java ME Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In in int data 0 in start makes no sense, that's why you are getting the output like that.  If you wish to print it like that, you have to us String/StringBuilder etc.
 
TushUpa Upah
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Swastik Dey wrote:In in int data 0 in start makes no sense, that's why you are getting the output like that.  If you wish to print it like that, you have to us String/StringBuilder etc.


Thanks. I have tried following. It has worked as expected:
public class x {

   public static String reverse(int n) {
       String input = String.valueOf(n);
       String result = "";
       for (int i = input.length() - 1; i >= 0; i--) {
           result = result + input.charAt(i);
       }
       return result;
   }

   public static void main(String[] args) {
       System.out.println("try with String");
       System.out.println(reverse(1230));
   }
}
 
Swastik Dey
Rancher
Posts: 1965
15
Android Java ME Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We are always here to help you.  Although concatenation of string is a not a very good practice.  Use StringBuilder and use append method of that class.  You can even reverse it using one to two lines of codes by calling reverse method of StringBuilder.
 
Sheriff
Posts: 6128
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags (that's a link) when posting code.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!