Reversing a Number Without Strings

  • Context: Comp Sci 
  • Thread starter Thread starter Funkmaster W
  • Start date Start date
  • Tags Tags
    Strings
Click For Summary

Discussion Overview

The discussion revolves around creating a program to reverse the digits of an integer without using string variables. Participants explore different programming approaches, particularly in Java, and consider the implications of using strings versus integers in their solutions.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant presents a working Java code that reverses digits but uses a string variable, questioning if it can be done without strings.
  • Another participant suggests that the program should output an integer rather than a string and describes a method to parse the input number to reverse its digits mathematically.
  • A different viewpoint advocates for using strings for simplicity, providing pseudo-code for reversing the digits by manipulating string characters.
  • One participant shares an alternative approach that involves using mathematical operations and counters, while acknowledging the use of a string method for length determination.

Areas of Agreement / Disagreement

Participants express differing opinions on whether to use string variables or to strictly adhere to integer manipulation. No consensus is reached on the best approach, and multiple competing views remain.

Contextual Notes

Some participants mention the limitations of their approaches, such as the reliance on string methods or the potential inefficiency of certain mathematical techniques. There is also a discussion about the maximum integer size in Java, which may affect the implementation.

Funkmaster W
Messages
12
Reaction score
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
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.
 
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.)
 
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.
 
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
 

Similar threads

  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 2 ·
Replies
2
Views
18K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
9
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K