Computer Applications
Write a menu driven program to calculate:
- Area of a circle = p*r2, where p = (22/7)
- Area of a square = side*side
- Area of a rectangle = length*breadth
Enter 'c' to calculate area of circle, 's' to calculate area of square and 'r' to calculate area of rectangle.
Java
Java Conditional Stmts
222 Likes
Answer
import java.util.Scanner;
public class KboatMenuArea
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter c to calculate area of circle");
System.out.println("Enter s to calculate area of square");
System.out.println("Enter r to calculate area of rectangle");
System.out.print("Enter your choice: ");
char choice = in.next().charAt(0);
switch(choice) {
case 'c':
System.out.print("Enter radius of circle: ");
double r = in.nextDouble();
double ca = (22 / 7.0) * r * r;
System.out.println("Area of circle = " + ca);
break;
case 's':
System.out.print("Enter side of square: ");
double side = in.nextDouble();
double sa = side * side;
System.out.println("Area of square = " + sa);
break;
case 'r':
System.out.print("Enter length of rectangle: ");
double l = in.nextDouble();
System.out.print("Enter breadth of rectangle: ");
double b = in.nextDouble();
double ra = l * b;
System.out.println("Area of rectangle = " + ra);
break;
default:
System.out.println("Wrong choice!");
}
}
}
Variable Description Table
Program Explanation
Output
![BlueJ output of Write a menu driven program to calculate: (a) Area of a circle = p*r 2 , where p = (22/7) (b) Area of a square = side*side (c) Area of a rectangle = length*breadth Enter 'c' to calculate area of circle, 's' to calculate area of square and 'r' to calculate area of rectangle. BlueJ output of Write a menu driven program to calculate: (a) Area of a circle = p*r 2 , where p = (22/7) (b) Area of a square = side*side (c) Area of a rectangle = length*breadth Enter 'c' to calculate area of circle, 's' to calculate area of square and 'r' to calculate area of rectangle.](https://cdn1.knowledgeboat.com/img/apc10/unit_7-p9_1.jpg)
![BlueJ output of Write a menu driven program to calculate: (a) Area of a circle = p*r 2 , where p = (22/7) (b) Area of a square = side*side (c) Area of a rectangle = length*breadth Enter 'c' to calculate area of circle, 's' to calculate area of square and 'r' to calculate area of rectangle. BlueJ output of Write a menu driven program to calculate: (a) Area of a circle = p*r 2 , where p = (22/7) (b) Area of a square = side*side (c) Area of a rectangle = length*breadth Enter 'c' to calculate area of circle, 's' to calculate area of square and 'r' to calculate area of rectangle.](https://cdn1.knowledgeboat.com/img/apc10/unit_7-p9_2.jpg)
Answered By
76 Likes
Related Questions
Write a program to input a number and check whether it is a perfect square or not. If the number is not a perfect square then find the least number to be added to input number, so that the resulting number is a perfect square.
Example 1:
Sample Input: 2025
√2025 = 45
Sample Output: It is a perfect square.
Example 2:
Sample Input: 1950
√1950 = 44.1588……..
Least number to be added = 452 - 1950 = 2025 - 1950 = 75Write a program that accepts three numbers from the user and displays them either in "Increasing Order" or in "Decreasing Order" as per the user's choice.
Choice 1: Ascending order
Choice 2: Descending orderSample Inputs: 394, 475, 296
Choice 2
Sample Output
First number : 475
Second number: 394
Third number : 296
The numbers are in decreasing order.Write a program using switch case to find the volume of a cube, a sphere and a cuboid.
For an incorrect choice, an appropriate error message should be displayed.- Volume of a cube = s * s *s
- Volume of a sphere = (4/3) * π * r * r * r (π = (22/7))
- Volume of a cuboid = l*b*h
The relative velocity of two trains travelling in opposite directions is calculated by adding their velocities. In case, the trains are travelling in the same direction, the relative velocity is the difference between their velocities. Write a program to input the velocities and length of the trains. Write a menu driven program to calculate the relative velocities and the time taken to cross each other.