- #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.
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.