Simple calculator program in php using functions

This is a simple calculator that can perform the simple arithmetic tasks (+, -, * and /).

Write a simple calculator program in PHP using if-else statement

HTML Code for Simple Calculator

<h2>Simple Calculator</h2><br>

First Number:<input name="n1"value=""><br>

Second Number:<input name="n2"value=""><br>

<input type="submit"name="sub"value="+">

<input type="submit"name="sub"value="-">

<input type="submit"name="sub"value="x">

<input type="submit"name="sub"value="/"><br>

<br>Result:<input type='text'value=" echo$ans;?>"><br>

</form>

PHP Code for Simple Calculator

We are using Else-If statement for performing the functions.

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

$num1=$_POST['n1'];

$num2=$_POST['n2'];

$oprnd=$_POST['sub'];

if($oprnd=="+")

$ans=$num1+$num2;

elseif($oprnd=="-")

$ans=$num1-$num2;

elseif($oprnd=="x")

$ans=$num1*$num2;

else if($oprnd=="/")

$ans=$num1/$num2;

}?>

Output:

Simple calculator program in php using functions

Download Code – Simplecalculator


Write a simple calculator program in PHP using the switch statement

Operations on calculator

Following operations will perform on the given calculator;

  1. Multiplication
  2. Division
  3. sum
  4. Subtraction

Description:

You need to write a simple calculator program in PHP using switch case.

Operations:

  • Addition
  • Subtraction
  • Multiplication
  • Division

View Solution/Program


 

	Simple Calculator Program in PHP - Tutorials Class

 

 

    

PHP - Simple Calculator Program

First Number

Second Number

Result

Tutorials Class - Output Window

Simple calculator program in php using functions


Learn more about the similar topics:

Tutorials
PHP Decision Making
Exercises & Assignments
Write a program to check student grade based on marks
Write a program to show day of the week using switch
Write a program to calculate Electricity bill in PHP
Write a simple calculator program in PHP using switch case
Write a PHP program to check if a person is eligible to vote
Write a PHP program to check whether a number is positive, negative or zero
Interview Questions & Answers
No Content Found.

How to design a simple calculator in PHP?

PHP Code for Simple Calculator php if(isset($_POST['sub'])){ $num1=$_POST['n1']; $num2=$_POST['n2']; $oprnd=$_POST['sub']; if($oprnd=="+") $ans=$num1+$num2; else if($oprnd=="-") $ans=$num1-$num2; else if($oprnd=="x") $ans=$num1*$num2; else if($oprnd=="/") $ans=$num1/$num2; }?>

How do you make a calculator with JavaScript?

const number1 = parseFloat(prompt ('Enter the first number: ')); const number2 = parseFloat(prompt ('Enter the second number: ')); let result; // declaration of the variable. // use if, elseif and else keyword to define the calculator condition in JavaScript.