Reversing a number using Java

Submitted by: 
Language: 
Visitors have accessed this post 1209 times.

Program code requests a user to enter a number then outputs the result as the number reversed. Say, the original number is 1234, the output will be 4321.

  1. Using eclipse
  2. import java.util.Scanner;
  3.  
  4. public class ReverseNumber {
  5. public static void main(String [] args){
  6. System.out.println("Please enter a number: ");
  7. int number = new Scanner(System.in).nextInt();
  8.  
  9. int reversedNumber, i;
  10.  
  11. reversedNumber = 0;
  12. i = 0;
  13.  
  14. while(number > 0){
  15. /*this will get the remainder of the number which will be
  16. *the rightmost digit
  17.   */
  18. i = number % 10;
  19.  
  20. //this creates the reversed number
  21. reversedNumber = reversedNumber * 10 + i;
  22.  
  23.  
  24. //The result will be stored as an integer value
  25. number = number/10;
  26. }
  27.  
  28. System.out.println("The number reversed is: +reversedNumber);
  29. }
  30. }
  31.  
  32. Using a text editor & command prompt
  33. import java.io.*;
  34.  
  35. class ReverseNumber{
  36. public static void main(String args[]) {
  37. try {
  38. BufferedReader s =
  39. new BufferedReader(new InputStreamReader(System.in));
  40. int number, reversednumber, i;
  41.  
  42. System.out.print("Enter the number to be reversed: ");
  43. number = Integer.parseInt(s.readLine());
  44.  
  45. i = 0;
  46. reversednumber = 0;
  47.  
  48. while(number > 0){
  49. i = number % 10;
  50. reversednumber = reversednumber * 10 + i;
  51. number = number/10;
  52. }
  53. System.out.print("The number reversed: " + reversednumber);
  54. }
  55. catch(IOException e){}
  56. }
  57.  
  58. }

Tags: 

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Comments

a great code my friend

but if u know StringBuilder class , its much easier then
u can read the number as a string
then using StringBuilder u can use the reverse method and then print it out
example :

String reverse (String number ) {
return (new StringBuilder(str).reverse().toString()) ;
}

Computer Science Department - Birzeit Uni
follow me on
twitter : @SJinat
email : [email protected]

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.