wap to input in java using switch case
Answers
class area
{
public static void main(String args[ ])
{
InputStreamReader read= new InputStreamReader(System.in)
Buffered Reader in= new BufferedReader(read);
double s, m, n, p, a, b, area; int ch;
System.out.println("enter 1 for equi tria, enter 2 for second tria, enter 3 for scalene tria);
Sytem.out.println("enter your choice");
ch=Integer.parseInt(in.readLine());
switch(ch)
{
case 1:
System.out.println("enter the side of a traiangle");
s=Double.parseDouble(in.readLine());
area=(Math.sqrt(3)/4)*(Math.pow(s, 2);
System.out.println("area="+area);
break;
case 2:
System.out.println("enter a and b");
a=Double.parseDouble(in.readLine());
b=Double.parseDouble(in.readLine());
area=1/4*b*(Math.sqrt(4*Math.pow(a, 2)-Math.pow(b, 2);
||
System.out.println("area="+area);
break;
case 3:
System.out.println("enter m, n, p");
m=Double.parseDouble(in.readLine());
n=Double.parseDouble(in.readLine());
p=Double.parseDouble(in.readLine());
s=(m+n+p)/2
area=Math.sqrt(s*(s-m)*(s-n)*(s-p));
System.out.println("area="+area);
break;
}
}
}
—› Your Program:
//java program to calculate the area of triangles
import java.util.*;
public class Area
{
public static void main (String args[] )
{
Scanner in = new Scanner(System.in); //using scanner to take input from user
int c, s, a, b, m, n, p;
double t, ar=0;
System.out.println("1. Area of an equilateral triangle" );
System.out.println("2. Area of an isosceles triangle" );
System.out.println("3. Area of a scalene triangle" );
System.out.println("Enter your choice:" );
c = in.nextInt();
switch(c)
{
case 1:
System.out.println("Enter the side of an equilateral triangle:" );
s = in.nextInt();
ar= (Math.sqrt(3)*s*s)/4;
System.out.println("Area =" +ar);
break;
case 2:
System.out.println("Enter the side & base of isosceles triangle:" );
a = in.nextInt();
b = in.nextInt();
ar = b/4.0*(Math.sqrt(4*a*a-b*b));
System.out.println("Area =" +ar);
break;
case 3:
System.out.println("Enter the sides of scalene triangle:" );
m = in.nextInt();
n = in.nextInt();
p = in.nextInt();
t = (m+n+p)/2.0;
ar = Math.sqrt(t*(t-m)*(t-n)*(t-p));
System.out.println("Area =" +ar);
break;
default:
System.out.println("Wrong Choice!!" );
}
}
}
—› Output:
1. Area of an equilateral triangle
2. Area of an isosceles triangle
3. Area of a scalene triangle
Enter your choice:
2
Enter the side & base of isosceles triangle:
9
11
Area = 39.1814687
---------------------------------------------