Computer Science, asked by esthercanarenald, 2 months ago

Write a menu driven program using switch-case to find the volume of cube, cone or a cylinder

as per the user’s choice. An appropriate error message should be displayed for an incorrect

choice. (take π = 3.14)

1. Volume of cube: s*s*s

2. Volume of cone:



*π*r*r*h

3. Volume of cylinder: π*r*r*h​

Answers

Answered by BrainlyProgrammer
2

Question:-

  • Write a menu driven program using switch-case to find the volume of cube, cone or a cylinder as per the user’s choice. An appropriate error message should be displayed for an incorrect choice. (take π = 3.14)
  1. Volume of cube: s³
  2. Volume of cone: πr²h/3
  3. Volume of cylinder: πr²h

Code:-

package Coder;

import java.util.*;

public class SwitchVolume {

 public static void main(String[] args) {

 Scanner sc=new Scanner (System.in);

 System.out.println("Enter your choice:-\n'1' for cube\n'2'for cone\n'3'for cylinder");

 int ch=sc.nextInt();

 double vol=0.0,h,r;

 switch(ch)

 {

   case 1:

   System.out.println("-----Volume of cube-----");

   System.out.println("Enter the side of the cube");

   r=sc.nextDouble();

   vol=Math.pow(r,3);

   break;

   case 2:

   System.out.println("-----Volume of cone-----");

   System.out.println("Enter the radius of the cone");

   r=sc.nextDouble();

   System.out.println("Enter the height of the cone");

   h=sc.nextDouble();

   vol=3.14*Math.pow(r,2)*h/3;

   break;

   case 3:

   System.out.println("-----Volume of cylinder-----");

   System.out.println("Enter the radius of the cylinder");

   r=sc.nextDouble();

   System.out.println("Enter height of the cylinder");

   h=sc.nextDouble();

   vol=3.14*Math.pow(r,2)*h;

   break;

   default:System.out.println("Invalid choice");

 }

 System.out.println("Volume:"+vol);

 }

}

Variable Description:-

  1. ch:- To Accept choice as input from the user
  2. r:- To Accept radius(for cylinder/cone)/side(for cube) from the user
  3. h:- To Accept height of the cone/cylinder from the user
  4. vol:- to calculate and store the volume

•Output Attached

Attachments:
Similar questions