The program shows how to Calculate the Area of Different Geometric Figures in java using a switch statement. The program uses a scanner class to take the inputs from the user.
The switch case is mostly used when it is necessary to give options to the users. The following program gives users seven cases or options to choose from and it displays results accordingly.
If you do not know the working process of the switch case statement then click the link below.
Java Program to Calculate the Area of Different Geometric Figures using Switch Case
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | //area of different geometric figures using switch case in java import java.util.Scanner; public class AreasSwitch { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("To find the areas of following press:"); System.out.println("1 for the Area of a Circle"); System.out.println("2 for the Area of a Square"); System.out.println("3 for the Area of a Right Angled Triangle"); System.out.println("4 for the Area of a Rectangle"); System.out.println("5 for the Circumference of a Circle"); System.out.println("6 for the Perimeter of a Square"); System.out.println("7 for the Perimeter of a Rectangle"); System.out.println("Enter your Choice:"); int ch = sc.nextInt(); switch(ch) { case 1: System.out.println("Enter radius for the circle:"); float r = sc.nextFloat(); float area = 3.14f*r*r; System.out.println("Area:"+area); break; case 2: System.out.println("Enter side for square:"); int s = sc.nextInt(); int ae = s*s; System.out.println("Area:"+ae); break; case 3: System.out.println("Enter height and base for traingle:"); float h = sc.nextFloat(); float bs = sc.nextFloat(); float ar = 0.5f*h*bs; System.out.println("Area:"+ar); break; case 4: System.out.println("Enter length and breadth for rectangle:"); int l = sc.nextInt(); int b = sc.nextInt(); int aa = l*b; System.out.println("Area:"+aa); break; case 5: System.out.println("Enter radius for circle:"); float rad = sc.nextFloat(); float cir = 3.14f*2f*rad; System.out.println("Circumference:"+cir); break; case 6: System.out.println("Enter side for square:"); int side = sc.nextInt(); int peri = 4*side; System.out.println("Perimeter:"+peri); break; case 7: System.out.println("Enter length and breadth for rectangle:"); int lg = sc.nextInt(); int br = sc.nextInt(); int per = 2*(lg+br); System.out.println("Perimeter:"+per); break; default:System.out.println("Invalid Input"); break; } } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | To find the areas of following press: 1 for the Area of a Circle 2 for the Area of a Square 3 for the Area of a Right Angled Triangle 4 for the Area of a Rectangle 5 for the Circumference of a Circle 6 for the Perimeter of a Square 7 for the Perimeter of a Rectangle Enter your Choice: 4 Enter length and breadth for rectangle: 5 8 Area:40 |