Write a program in Java to accept the diagonal of a square, find and display the
area & perimeter.
Answers
Explanation:
this is your answers.......
Question:-
➡ Write a program to accept the length & breadth of a rectangle. Calculate & display the area, perimeter or diagonal of the rectangle as per the user’s choice.
Program:-
This is written in Java.
import java.util.*;
class Choice
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
double l, b;
int ch;
System.out.print("Enter the length of the rectangle: ");
l=sc.nextDouble();
System.out.print("Enter the breadth of the rectangle: ");
b=sc.nextDouble();
System.out.println("1. Calculate Area.");
System.out.println("2. Calculate Perimeter.");
System.out.println("3. Calculate Diagonal.");
System.out.print("\nEnter your choice: ");
ch=sc.nextInt();
switch(ch)
{
case 1:
System.out.println("Area of the rectangle is: "+a*b);
break;
case 2:
double p=2*(l+b);
System.out.println("Perimeter of the rectangle is: "+p);
break;
case 3:
double d=Math.sqrt(l*l+b*b);
System.out.println("Diagonal of the rectangle is: "+d);
break;
default: System.out.println("Invalid Choice. Please try again later.");
}
}
}