Ad Code

Write a program to find area of square in java source code by using Switch case class 8

 Area of square in java source code

Certainly! Here's an article explaining how to use a `switch` statement in Java to perform different operations like calculating the side length, area, or perimeter of a square based on user input

**Mastering Square Operations in Java: Using `switch` to Calculate Side Length, Area, and Perimeter**

Java, the versatile programming language, offers a multitude of ways to tackle real-world problems. we'll explore how to create a Java program that calculates various properties of a square based on user input. By employing a `switch` statement, we can seamlessly switch between tasks and perform operations like finding the side length, area, or perimeter of a square at the press of a button.


**Understanding the Square**

Before we dive into the code, let's clarify what we're dealing with.square is a quadrilateral with four equal sides and four right angles. The essential properties we can calculate are:

1. **Side Length**: The length of one side of the square.

2. **Area**: The total space enclosed by the square.

3. **Perimeter**: The total length of all four sides of the square.


**Coding the Square Operations Program**

Source code of Program  

import java.util.Scanner;

class pro

{

    public static void main(String Ar[])

    {

        Scanner obj=new Scanner(System.in);

        System.out.println("1.side of the square");

        System.out.println("2.area of the square");

        System.out.println("3.perimeter of the square");

        System.out.println("enter user choice");

        int ch=obj.nextInt();

        switch(ch)

        {

            case 1:

                   System.out.println("enter the length");

                   double s=obj.nextDouble();

                   System.out.println("enter the breadth");

                   double b=obj.nextDouble();

                   System.out.println("length of the rectangle="+s);

                   System.out.println("breadth of the rectangle="+b);

                   break;

                   case 2:

                      System.out.println("enter the length");

                   double l=obj.nextDouble();

                   System.out.println("enter the breadth");

                   double b1=obj.nextDouble();         

                   double a=l*b1;

                   System.out.println("area of the rectangle="+a);

                   break;

                   case 3:

                       System.out.println("enter the length");

                        double s2=obj.nextDouble(); 

                         System.out.println("enter the breadth");

                   double b3=obj.nextDouble();

                        double per=2*(s2+b3);

                        System.out.println("perimeter of the rectangle="+per);

                        break;

                        default:

                            System.out.println("exit");

        }

    }

}

Download Source Code

**How the Program Works**

1. The program starts by displaying a menu of options to the user: 1 for side length, 2 for area, and 3 for perimeter.


2. The user enters their choice, and the `switch` statement handles the selected operation.


3. Based on the user's input, the program prompts for the necessary data (side length or area) and performs the corresponding calculation.


4. Finally, it displays the result to the user.


**Conclusion**


With the power of a `switch` statement, this Java program effortlessly switches between calculating different properties of a square. Whether you need to find the side length, area, or perimeter, this program simplifies the process with just a few button presses. It's a testament to Java's flexibility in solving everyday mathematical challenges.


Feel free to modify and expand this program for your specific needs or experiment with other geometric shapes. Happy coding.

I hope you find this article helpful for understanding how to use a `switch` statement in Java to perform different tasks based on user input. you have any further questions or requests, please let me know

Output Screen 


Post a Comment

0 Comments

Ad Code

Responsive Advertisement