0
if a five-digit number is input through the keyboard, write a program to reverse the number???
plz solve and also explain it....
4 ответов
+ 2
lets say the number entered is 12345...
long Value = scanner.nextLong();
String convertValue = Long.toString(Value);
StringBuffer buffer = new StringBuffer();
for(int i = convertValue.length()-1; i >=0; i--){
buffer.append(convertValue.charAt(i));
}
String finalAns = new String(buffer);
long finalAnswer = Long.parseLong(finalAns);
System.out.println(finalAnswer);
+ 1
import java.util.Scanner;
class ReverseNumberWhile
{
public static void main(String args[])
{
int num=0;
int reversenum =0;
System.out.println("Input your number and press enter: ");
//This statement will capture the user input
Scanner in = new Scanner(System.in);
//Captured input would be stored in number num
num = in.nextInt();
//While Loop: Logic to find out the reverse number
while( num != 0 )
{
reversenum = reversenum * 10;
reversenum = reversenum + num%10;
num = num/10;
}
System.out.println("Reverse of input number is: "+reversenum);
}
}
I feel its best
0
I'm beginner. so I can't understand this way, plzz solve a simple way and explain loop.
0
There are many ways to do this, I think that is one simple one, there is not really an loop for it!