Php program for arithmetic operations using switch case

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    We are going to perform the basic arithmetic operations like- addition, subtraction, multiplication, and division using PHP. We are using HTML form to take the input values and choose an option to perform particular operation using Switch Case.

    Arithmetic Operations are used to perform operations like addition, subtraction etc. on the values. To perform arithmetic operations on the data we need at least two values.

    • Addition: It performs the sum of given numbers.
    • Subtraction: It performs the difference of given numbers.
    • Multiplication: It performs the multiplication of given numbers.
    • Division: It performs the division of given numbers.

    Example:

    Addition = val + val2 + ... + valn
    Example: add = 4 + 4 = 8
    
    Subtraction = val - val2 - ... - valn
    Example: sub = 4 - 4 = 0
    
    Multiplication = val1 * val2 * ... * valn
    Example: mul = 4 * 4 = 16
    
    Division = val1 / val2
    Example: mul = 4 / 4 = 1

    Program 1:

    PHP

    $x = 15;

    $y = 30;

    $add = $x + $y

    $sub = $x - $y;

    $mul = $x * $y;

    $div = $y / $x

    echo "Sum: " . $add . "\n";

    echo "Diff: " . $sub . "\n";

    echo "Mul: " . $mul . "\n";

    echo "Div: " . $div;

    ?>

    Output:

    Sum: 45
    Diff: -15
    Mul: 450
    Div: 2

    Using Switch Case: The switch statement is used to perform different actions based on different conditions.

    Syntax:

    switch (n) {
        case label1:
            code to be executed if n=label1;
            break;
        case label2:
            code to be executed if n=label2;
            break;
        case label3:
            code to be executed if n=label3;
            break;
    
        . . .
        
        case labeln:
            code to be executed if n=labellast;
            break;
        default:
            code to be executed if n is different from all labels;
    }

    Execution Steps:

    • Start XAMPP Server 
       

    Php program for arithmetic operations using switch case

    • Open Notepad and type the below code and save the folder in path given in image 
       

    Program 2:

    PHP

        GFG

        

            ARITHMETIC OPERATIONS DEMO USING

            SWITCH CASE IN PHP

        

        

    Option-1 = Addition

        

    Option-2 = Subtraction

        

    Option-3 = Multiplication

        

    Option-4 = Division

        

    "post">

            

    "0">

                

                    

                    

                

                

                

                

                

                

                    

                    

                

                

                    

                

            

    "text" name="num1"

                        value="" placeholder="Enter value 1"/>

                    

    "text" name="num2" value=""

                        placeholder="Enter value 2"/>

                    

    "text" name="option" value=""

                        placeholder="Enter option 1-4 only"/>

                    

    "submit" name="submit"

                        value="Submit"/>

                    

        

    if(isset($_POST['submit'])) {

        $a = $_POST['num1'];

        $b = $_POST['num2'];

        $ch = $_POST['option'];

        switch($ch) {

            case 1:

                $r = $a + $b;

                echo " Addition of two numbers = " . $r ;

                break;

            case 2:

                $r = $a - $b;

                echo " Subtraction  of two numbers= " . $r ;

                break;

            case 3:

                $r = $a * $b;

                echo " Multiplication of two numbers = " . $r ;

                break;

            case 4:

                $r = $a / $b;

                echo " Division of two numbers = " . $r ;

                break;

            default:

                echo ("invalid option\n");

        }

        return 0;

    }

    ?>

    Output: Open Web Browser and type localhost/gfg/code.php

    Addition

    Subtraction

    Multiplication

    Division


    Can we use arithmetic operations in switch case?

    Can we use arithmetic operators in switch case: Yes, We Can use we. To provide symbols in a case statement a single quote is used. To perform arithmetic operations, we create a case for each operator, for the matching operator, it will call the appropriate case statements and execute the statements.

    What is arithmetic operator in PHP with example?

    PHP Arithmetic Operators.

    What are the 5 arithmetic operators?

    Definition. The arithmetic operators perform addition, subtraction, multiplication, division, exponentiation, and modulus operations.

    What is arithmetic operation in programming?

    Arithmetic Operation - is a function which can perform one of the following tasks: adding, subtracting, multiplying and dividing. Operator - a programming device that performs a function on one or more inputs, for example arithmetic operators (+,-,/,*)