Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7052065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:20:20+00:00 2026-05-28T03:20:20+00:00

So I am learning Java and doing a bit of basic algorithms (still really

  • 0

So I am learning Java and doing a bit of basic algorithms (still really new to this).

Say the requirement is to reverse an integer of 5143 so it becomes 3415.

Which of the following is the better way to do it (or is there an even better way)? The two different functions are reverse() and reverseNum (Ability to reverse 0 value is neglected):

public class ReverseIntegers {

public static void main(String[] args) {

reverseNum(5143);
reverse(5143);

}

public static void reverse(int number) {

    String numString = Integer.toString(number);
    String result = "";

    char[] cArray = numString.toCharArray();

    for (int i = (cArray.length - 1); i >= 0; i--) {
        result += Character.toString(cArray[i]);    // concatenate the String numbers in reverse order

    }

    System.out.println(Integer.parseInt(result));

}

public static void reverseNum(int number) {

    int numLength = Integer.toString(number).length();

    int[] numArray;
    numArray = new int[numLength];

    int numMod;
    int numModLength;
    int result = 0;


    for (int i = numLength - 1; i >= 0; i--) {

        numMod = (int)(number % Math.pow(10, i + 1));    // eliminate first integer value one by one
        numModLength = Integer.toString(numMod).length();

        while (numModLength > 1) {    // obtain the first integer value of the remainder
            numMod = (numMod / 10); 
            numModLength--;
        }

        numArray[i] = numMod;    // assign the first integer value to the array
    }

    int digits = numArray.length - 1;

    for (int i = 0; i < numArray.length; i++) {    // put numbers in the array in reverse sequence

        result += numArray[i] * Math.pow(10, digits);
        digits--;

    }

    System.out.println(result);

}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T03:20:21+00:00Added an answer on May 28, 2026 at 3:20 am

    The first function is roughly equivalent to:

    String rev = new StringBuilder().append(number).reverse().toString();
    System.out.println(Integer.parseInt(rev));
    

    The second function is (in my opinion) quite hard to read. It is also quite inefficient due to the repeated calls to Integer.toString().

    A third alternative (which you haven’t coded up) is to have a function that’s purely based on integer maths and doesn’t use strings at all. It’ll probably be the most efficient of the lot. It will, however, require some care in how it deals with numbers that end in zeroes.

    In my view the contest is between the first and the third alternatives; the second function has the shortcomings of both and the advantages of neither.

    In the absence of any further information, I’d probably use the code at the start of this answer, and would replace it with something that doesn’t using strings if and only if profiling the app suggests it’s worth doing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm learning Java and OOPS from oracle's website. I'm doing this exercise . I'm
This is for a homework I'm doing on my walk learning java. I'm writing
Continuing my quest of learning Java by doing a simple game, i stumbled upon
I'm learning Java and OOP, and have been doing the problems at Project Euler
I am very new to this site and to programming. I started doing some
I am learning Java, now working with arrays. I am doing an excercise where
Still in the early stages of learning Java and I saw the following in
I'm learning scala and can't find out how to do this: I'm doing a
I've only started learning Java about 3 months ago and this is my first
I'm learning programming with Java. This is my solution to the second problem in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.