Hello Friends, Today I will tell you through experts php tutorial how you can create a calculator of any number of natural sum by php program code. Today you will be told by this article. So let’s go.
How to Calculate sum of first n natural numbers formula with Example?
First of all you have been told that the math formula with the help of how to calculate first n sum natural numbers. The formula is explained below by example.
An efficient solution is to use below formula.
Sum of first n natural numbers = (n*(n+1))/2
Example :
n = 5
sum = (5(5+1))/2 = (5*6)/2 = 30/2 = 15
or
n = 15
sum = (10(15+1))/2 = (10*16)/2 = 160/2 = 80
How to Calculate Sum of Natural Numbers Using PHP With Example?
Now how will you find the sum of natural numbers with the help of php. You are told by example below. I will explain you how to find the sum of natural numbers by 2 method.
First Method With Example
In the first method, you are told that you can calculate the sum of natural numbers with the help of a formule containing math by creating a function.
<?php
// Returns sum of first n natural
// numbers
function findSum($n)
{
return ($n * ($n + 1) / 2);
}
$n = 10;
echo findSum($n);
?>
Second Method With Example
In the second method, you have been told that you can calculate the sum of natural numbers with the help of a loop by creating a function of php.
<?php
// Returns sum of first n natural
// numbers
function findSum($n)
{
$sum = 0;
for ($x = 1; $x <= $n; $x++)
$sum = $sum + $x;
return $sum;
}
$n = 10;
echo findSum($n);
?>
How to use sum of natural numbers calculator in php file?
You have been told the php code to calculate the sum of natural numbers above. Now I will tell you step to step how you will use the code to calculate the sum of natural numbers in the php file.First of all you create a php file. You can name this file in your own way like naturalnumber.php.In this file, you add the code of html. The way is described below.
<html>
<head>
<meta charset="utf-8">
<title>Sum of Natural Numbers Calculator</title>
</head>
<body>
<form method="post">
<p>Value:<br/>
<input type="text" name="num"></p>
<button type="submit" name="submit" value="Calculate">Calculate Natural Number</button>
</form>
</body>
</html>
Now how do you calculate the sum of natural numbers by php. Its code is written well below. You have to use this code in the file of naturalnumber.php. First of all, understand how the sum of natural numbers is calculated.
<?php
if (isset($_POST['submit'])) {
$n = $_POST['num'];
if(empty($n)) {
echo "empty";
} else {
function findSum($n)
{
return ($n * ($n + 1) / 2);
}
$n = $_POST['num'];
echo findSum($n);
}
}
?>
So far, whatever you have been told above, you may have understood all the topics. Now you have to add this code to a single file. The example of which is given below. You can copy the code of this file to your php You can use it by adding it to the file.
naturalnumber.php with proper code
<html>
<head>
<meta charset="utf-8">
<title>Sum of Natural Numbers Calculator</title>
</head>
<body>
<form method="post">
<p>Value:<br/>
<input type="text" name="num"></p>
<button type="submit" name="submit" value="Calculate">Calculate Natural Number</button>
</form>
</body>
</html>
<?php
if (isset($_POST['submit'])) {
$n = $_POST['num'];
if(empty($n)) {
echo "empty";
} else {
function findSum($n)
{
return ($n * ($n + 1) / 2);
}
$n = $_POST['num'];
echo findSum($n);
}
}
?>
In the code above, there are two examples showing you how this function works.I hope that you found this function as useful as I did.