Arithmetic Caculation

PHP Addition of two numbers: PHP program

PHP Addition of two numbers: PHP program

In this article, we will discuss the concept of PHP Addition of two numbers

In this post, we are going to learn how to write a program to find  addition of two numbers in PHP language

Code to sum  of two numbers in PHP

Addition of two numbers in PHP

In this code, we are going to learn how to write a program to addition of two numbers in PHP  programming language

Program 1

  • Start PHP script
  • Declare and initiates two integer variable
  • Calculate sum of given two numbers
  • Display result on the screen
  • Exit PHP

In this program, the user initiates the values to variables and calculates the sum of the given values using plus(+) operator

<?php
$num1=5;
$num2=10;
$sum=$num1+$num2;
  echo "Sum: ",$sum;
?>

When the above code is executed it produces the following  result

Sum: 15

 

Division of two numbers in PHP using third  variable

In this program, the user initiates the values to variables and calculates the sum off the given numbers using plus(+) operator .Then the result is assigned to third variable (msg) .finally the result is displayed on the screen.

Program  2

  • Start PHP script
  • Declare and initiates two integer variable
  • Calculate sum of given two numbers
  • Assign the result to third variable
  • Display result on the screen
<?php
$x=5;
$y=10;
$sum=$x+$y;
$msg = 'Sum of x and y:' ;
print($msg.$sum);
?>

When the above code is executed it produces the following  result

Sum of x+y = 35

 

Division of two numbers in PHP -using form

In this program, the user is entered the values to variables using form(the program Asks input from the user to enter the form) and calculates the sum off the given numbers using plus(+) operator .Then the result is assigned to third variable (sum) .finally the result is displayed on the screen.

  • Create HTML form
  • Exit HTML form
  • Start PHP script
  • Exit PHP

Program 3

<html>
<head>
  <title>Sum of given  two numbers </title>
  </head>
  <body>
  <h3>Calculate  addition of given numbers</h3>

<form method="post">  
Enter 1st value:  
<input type="number" name="number1" /><br><br>  
Enter 2nd value:  
<input type="number" name="number2" /><br><br>  
<input  type="submit" name="submit" value="Add">  
</form>  



<?php  
    if(isset($_POST['submit']))  
    {  
        $number1 = $_POST['number1'];  
        $number2 = $_POST['number2'];  
        $sum =  $number1+$number2;     
echo "The sum of $number1 and $number2 is: ".$sum;   
}  
?> 

</body>
</html>

When the above code is executed it produces the following  result

Division of two numbers in PHP -without + operator

In this program, the user is entered the values to variables (the program Asks input from the user) and calculates the sum off the given values using without plus(+) operator .And then the result is displayed on the screen.

  • Create HTML form
  • Exit HTML form
  • Start PHP script
  • Exit PHP

Program 4

<html>
<head>
  <title>Addition of given  two numbers </title>
  </head>
  <body>
  <h3>Calculate  addition  of two numbers </h3>

<form>  
<b>Enter 1st number for sum:</b>
<input type="number" name="number1" /> <br> <br>
<b>Enter 2nd number for sum:  </b>
<input type="number" name="number2" />  </br>
<input  type="submit" name="submit" value="Sum">  
</form>  

</body>

<?php  
  @$number1 = $_GET['number1'];  
  @$number2 = $_GET['number2'];  
  for($i=1; $i<=$number2; $i++) 
  {
  $number1++;
    }   
echo "Sum of given two numbers".$number1
;     
?>  

When  the   above  code is  executed, it produces the  following result.

 

Enter number to form

 

click the sum button to  find  addition

Addition of two numbers:

 

Suggested for you

Java program to add two numbers

C program to add two numbers

C++ program to add two numbers

Python program to add two numbers

 

Write a program to sum of prime numbers 1 to n in Java language

Write a program to sum of prime numbers 1 to n in C language

Write a program to sum of prime numbers 1 to n in C++ language

Write a program to sum of prime numbers 1 to n in Python  language

 

Write a program to sum of natural numbers 1 to n in Java language

Write a program to sum of prime numbers 1 to n in C language

Write a program to sum of prime numbers 1 to n in C++ language

Write a program to sum of prime numbers 1 to n in Python  language

Karmehavannan

Recent Posts

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.