Write a Java program to reverse a number which is input by the user(through Buffered reader class)?
Answers
Solution:
The given cøde is written in Java.
import java.io.*;
public class Program {
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a number - ");
int n=Integer.parseInt(br.readLine());
int s=0;
while(n!=0){
s=s*10+n%10;
n/=10;
}
System.out.println("Reverse of the number - "+s);
}
}
Here, I have used BufferedReader class to take input.
Number n is taken as input and then the reversed number is calculated and stored in variable s. The variable s is now displayed on the screen.
See the attachment for output.
Answer:
Program:-
//To reverse a number using BufferedReader class
import java.io.*;
public class Main
{
public static void main(String args[])throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);// if you want a single inputted statenent also you may write BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
BufferedReader in=new BufferedReader(read);
int n,a=0,rev=0,num;
System.out.println("Enter a number");
n=Integer.parseInt(in.readLine());
num=n;
while(n!=0)
{
a=n%10;
rev=rev*10+a;
n=n/10;
}
System.out.println("The reversed form of "+num+"="+rev);
}
}