Write a program to accept 3 digit number and display the reverse of the number. Eg: Input number =345 Output number=543
Answers
Answer:
It can be easily done if you convert it to String and then use StringBuffer... I will do it by two methods:
1st: (without using string)
import java.util.Scanner;
class Reverse{
public static void main (String args[]){
Scanner sc = new Scanner (System.in);
System.out.println("Enter the number");
int n = sc.nextInt();
int result = 0 ;
while(n>0){
result = result*10 + n%10;
n=n/10;
}
System.out.println("The reversed number is "+result);
}
}
2nd: (using strings)
import java.util.Scanner;
import java.util.StringBuffer;
class Reverse{
public static void main (String args[]){
Scanner sc = new Scanner (System.in);
System.out.println("Enter the number");
int n = sc.nextInt();
StringBuffer num = new StringBuffer(n.toString());
num = num.reverse();
int result = Integer.parseInt(num.toString());
System.out.println("The reversed number is "+result);
}
}
Answer:
mark as brainlist please and Follow or thanks