Let’s learn java program to calculate area of circle rectangle and triangle using switch.
Java program to calculate area of circle rectangle and triangle using switch
We can use switch statement to calculate area of circle, rectangle and triangle.
import java.util.Scanner; public class FindAreaUsingSwitchStatement { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("MENU:"); System.out.println("1.Area of circle"); System.out.println("2.Area of triangle"); System.out.println("3.Area of rectangle"); System.out.println("Please enter any of the above option: "); int num = sc.nextInt(); switch(num) { case 1: System.out.println("Please enter radius of circle: "); double radius = sc.nextFloat(); double areaCircle = (22 * radius * radius) / 7; System.out.println("Area of circle is: " + areaCircle); break; case 2: System.out.println("Please enter base and height of triangle: "); double base = sc.nextFloat(); double height = sc.nextFloat(); double areaTriangle = (base* height) / 2; System.out.println("Area of triangle is: " + areaTriangle); break; case 3: System.out.println("Please enter length and breadth of rectangle: "); int length = sc.nextInt(); int breadth = sc.nextInt(); int areaRectangle = length * breadth; System.out.println("Area of ractangle is: " + areaRectangle); break; default:System.exit(0); } sc.close(); } }
Output:
MENU:
1.Area of circle
2.Area of triangle
3.Area of rectangle
Please enter any of the above option: 1
Please enter radius of circle: 5
Area of circle is: 78.57142857142857
MENU:
1.Area of circle
2.Area of triangle
3.Area of rectangle
Please enter any of the above option: 2
Please enter base and height of triangle:
20
100
Area of triangle is: 1000.0
MENU:
1.Area of circle
2.Area of triangle
3.Area of rectangle
Please enter any of the above option: 3
Please enter length and breadth of rectangle:
50
20
Area of ractangle is: 1000
Also read – operators in java