W.A.P in Java to accept the diagonal of the square find and display the area and perimeter by using by input stream
Answers
Answered by
4
Question:-
➡ Write a program in Java to accept the diagonal of a square and find and display the area and perimeter by using inputstream.
Program:-
import java.io.*;
class Square
{
public static void main(String args[])
{
try
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the diagonal of the square: ");
double d=Double.parseDouble(br.readLine());
/*
* Diagonal = Side*root 2
* So, side = diagonal/root 2
*/
double s=d/Math.sqrt(2);
s*=4;
System.out.println("Perimeter of the square is: "+s);
s/=4;
s*=s;
System.out.println("Area of the square is: " + s);
}
catch(Exception IO)
{
System.out.println("An error Occurred. Please try again later.");
}
}// main
}// class
Similar questions