Reversing a Number Without Strings

  • Comp Sci
  • Thread starter Funkmaster W
  • Start date
  • Tags
    Strings
In summary, the conversation discussed creating a program that would take a user inputted number and reverse the digits, outputting a different int value. The proposed solution involved parsing the input number and building a new number with the digits reversed. Different methods were suggested, such as storing the input as a string or using counters and math functions.
  • #1
Funkmaster W
12
0
Create a program in which the digits returned are reversed such as if 234 was the input the output is 432.

import java.util.*;
public class Test{
public static void main(String[] args){
String revNum ="";
int rem = 0;
int n;
Scanner scan = new Scanner(System.in);
System.out.println("Input a number greater than zero.");
n = scan.nextInt();
if(n<=0){
System.out.println("Input a number greater than zero.");
}
else{
while(n>0){
rem = n%10;
n = n/10;
revNum = ("" + revNum + rem);
}
System.out.println("Number Reversed:" + revNum);
}
}
}



Now technically this code works and does what it is supposed to but i was wondering if there was a way to do this without using any string variables.
 
Physics news on Phys.org
  • #2
Added [ code] and [ /code] around your code (without the extra spaces) so that your formatting is preserved.
Funkmaster W said:
Create a program in which the digits returned are reversed such as if 234 was the input the output is 432.
Code:
import java.util.*;
public class Test{
          public static void main(String[] args){
                String revNum ="";
                int rem = 0;
                int n;
                Scanner scan = new Scanner(System.in);
                System.out.println("Input a number greater than zero.");
                n = scan.nextInt();
                if(n<=0){
                    System.out.println("Input a number greater than zero.");
               }
               else{
               while(n>0){
                    rem = n%10;
                    n = n/10;
                    revNum = ("" + revNum + rem);
              }
              System.out.println("Number Reversed:" + revNum);
              }
        }
}


Now technically this code works and does what it is supposed to but i was wondering if there was a way to do this without using any string variables.
 
  • #3
In answer to your question, yes, this can and should be done without using string variables. In your code, the user inputs an int value, but outputs a string. What you program should do is output a different int value with the digits reversed.

To do this, your program would need to parse the input number and determine each of the digits of the ones' place, tens' place, hundreds' place, and so on up to the largest possible int value. After all the digits are determined the program could build a new number with the digits reversed.

For example, if the input number was 234, that's 2*100 + 3*10 + 4*1. The output number would be 4*100 + 3*10 + 2*1 = 432.

It's been a long while since I did any Java programming, but I suspect that an int in Java is four bytes, which would make the largest possible int either roughly 2 billion or 4 billion, depending on whether an int is signed or unsigned. Either way your program would have to be able to work with 10 digits: 1's, 10's, 100's, ..., up to the billions' digit. (I am using the US definition of 1 billion = 1,000 * 1,000,000.)
 
  • #4
Personally I would just read in the input and store it as a string rather than an integer. Although I'm not sure if you specifically have to do it like that for your question. Here's the pseudo-code:

- Store input as string
- Have another output string variable of just ""
- Loop through length of input string starting at the back using .charAt and just have the output string += the current .charAt character
- Print out output variable

If you need to later change it to an integer it would be very simple using Integer.parseInt(<outputVariable>);

edit: Sorry, not sure if I actually answered your question.
 
  • #5
Ok well, I'm not in the mood for thinking but I shouldn't really use that as an excuse. Anyway I thought of a somewhat inefficient way of doing it. It uses Math.pow and counters and what not. Anyway, I did what Mark44 said to do, BUT I had to use a String method .length(). Technically I didn't make a String variable though :D I'll help you on the logic if you don't understand :D
Code:
import java.util.Scanner;
public class Test {
	public static void main(String[] funkmasterWhelp){
		Scanner scan = new Scanner(System.in);
		int revNum = 0;
		int counter = 0;
		int num = 0;
		double power = 0;
		System.out.print("Enter a number larger than zero: ");//use print as it looks nicer
		num = scan.nextInt();
		/*
		 * You can do all of the error checking and all the other things
		 */
		counter = ("" + num).length()-1;
		while(num > 0){
			power = Math.pow(10.0,counter);
			revNum += num%10 * power;
			System.out.println(revNum);
			counter--;
			num /= 10;
			
		}
	}
}
YAY 100th post and it had to do something with Comp Sci! I'm happy! lol :D
 

FAQ: Reversing a Number Without Strings

What is reversing a number without strings?

Reversing a number without strings is the process of taking a numerical input and changing the order of its digits to create a new number. This is done without converting the number to a string or using any string manipulation methods.

Why would I want to reverse a number without strings?

Reversing a number without strings can be useful for a variety of reasons. It can be used to solve mathematical problems, such as finding the largest or smallest possible number from a given set of digits. It can also be used in programming to improve code efficiency and reduce the use of unnecessary data types.

What is the algorithm for reversing a number without strings?

The algorithm for reversing a number without strings involves using basic arithmetic operations, such as modulo and division, to extract and rearrange the digits of the input number. This can be done in a loop until all digits have been reversed, and then the reversed digits can be combined to form the new number.

Can any number be reversed without using strings?

Yes, any number can be reversed without using strings as long as it is within the range of the data type being used. For example, a 32-bit integer can be reversed without using strings, but a larger number may require a different approach.

What are some common challenges when reversing a number without strings?

One common challenge when reversing a number without strings is dealing with negative numbers. The algorithm may need to check for negative input and handle it differently. Another challenge is handling leading zeros, which may need to be removed before combining the reversed digits to form the new number.

Similar threads

Replies
7
Views
2K
Replies
12
Views
2K
Replies
7
Views
2K
Replies
2
Views
18K
Replies
2
Views
1K
Replies
4
Views
2K
Replies
1
Views
2K
Back
Top