Comp Sci Reversing a Number Without Strings

  • Thread starter Thread starter Funkmaster W
  • Start date Start date
  • Tags Tags
    Strings
AI Thread Summary
The discussion focuses on creating a Java program to reverse the digits of an integer input without using string variables. The initial code successfully reverses the number but outputs it as a string, prompting a request for an integer output instead. A proposed solution involves calculating each digit's place value using mathematical operations, specifically powers of ten, to reconstruct the reversed number. Several users share variations of the approach, with one suggesting the use of a counter to track the digit positions. Overall, the conversation emphasizes the importance of maintaining integer output while reversing the digits.
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
Views
2K
Replies
12
Views
2K
Replies
7
Views
3K
Replies
2
Views
1K
Replies
3
Views
2K
Replies
1
Views
2K
Back
Top