• Register now to get access to thousands of Tutorials, Leaked content, Hot NSFW and much more. Join us as we build and grow the community.

Advertise Here

Advertise Here

Advertise Here

Reversing a number using Java

Invernes

Process Workflow Enhancer
I Rep
0
0
0
Rep
0
I Vouches
0
0
0
Vouches
0
Posts
130
Likes
101
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 2 1000 XP
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. public

    class

    ReverseNumber {
  4. public

    static

    void

    main(

    String

    [

    ]

    args)

    {
  5. System

    .out

    .println

    (

    "Please enter a number: "

    )

    ;
  6. int

    number =

    new

    Scanner(

    System

    .in

    )

    .nextInt

    (

    )

    ;

  7. int

    reversedNumber, i;

  8. reversedNumber =

    0

    ;
  9. i =

    0

    ;

  10. while

    (

    number >

    0

    )

    {
  11. /*this will get the remainder of the number which will be
  12. *the rightmost digit
  13. */
  14. i =

    number %

    10

    ;

  15. //this creates the reversed number
  16. reversedNumber =

    reversedNumber *

    10

    +

    i;


  17. //The result will be stored as an integer value
  18. number =

    number/

    10

    ;
  19. }

  20. System

    .out

    .println

    (

    "The number reversed is: +reversedNumber);
  21. }
  22. }

  23. Using a text editor & command prompt
  24. import java.io.*;

  25. class ReverseNumber{
  26. public static void main(String args[]) {
  27. try {
  28. BufferedReader s =
  29. new BufferedReader(new InputStreamReader(System.in));
  30. int number, reversednumber, i;

  31. System.out.print("

    Enter the number to be reversed:

    ");
  32. number = Integer.parseInt(s.readLine());

  33. i = 0;
  34. reversednumber = 0;

  35. while(number > 0){
  36. i = number % 10;
  37. reversednumber = reversednumber * 10 + i;
  38. number = number/10;
  39. }
  40. System.out.print("

    The number reversed:

    " + reversednumber);
  41. }
  42. catch(IOException e){}
  43. }

  44. }

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.


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

Create an account or login to comment

You must be a member in order to leave a comment

Create account

Create an account on our community. It's easy!

Log in

Already have an account? Log in here.

452,502

355,786

355,794

Top