Factorial Program in PHP with example


Factorial Program in PHP with example

Hi friends, in this tutorial, you will learn how to run the factorial program in PHP. I will explain the details in a step-by-step process. This is a very simple program and you will never forget it if you read this article once.

Also, read the OOPs concept in PHP with real-time examples

Steps to writing the factorial program in PHP

Step 1:- First of all, enter the input value in the HTML form.

Step 2:- Now, we will receive the input value with the help of $_POST superglobal variable for which the factorial will be calculated.

Step 3:- Next, we will declare some variables as shown below

$fact = "";
$factorial_number_err = "";

Step 4:- Now, we will check if the input value is not empty then we will run a for loop up to the number we have received from the HTML form.

Step 5:- Now, each of the incremented values of the for loop will be multiplied with the $fact variable in every iteration and store the result in the $fact variable as shown below

Suppose you have to find a factorial of 5, then the for loop will be executed up to 5.

  • For '1', the value of the fact variable will be fact = fact*1 i.e. fact = 1*1 and it becomes face = 1;
  • For '2', the value of the fact variable will be fact = fact*2 i.e. fact = 1*2 and it becomes fact = 2;
  • For '3', the value of the fact variable will be fact = fact*3 i.e. fact = 2*3 and it becomes fact = 6;
  • For '4', the value of the fact variable will be fact = fact*4 i.e. fact = 6*4 and it becomes fact = 24;
  • For '5', the value of the fact variable will be fact = fact*5 i.e. fact = 24*5 and it becomes fact = 120

Step 6:- From the above step, you can see that the final factorial value of the entered number is 120.

Complete Code:-

<?php

	$factorial_number_err = "";
	$fact = "";

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

		if(!empty($_POST['fact_number']))
		{
			$factorial_number = $_POST['fact_number'];

			$fact = 1;
		 	for($i = 1; $i<=$factorial_number; $i++)
		 	{
		 		$fact = $i*$fact; 
		 		
		    }
		}
		else{
			$factorial_number_err = "Input is missing !";
			$fact = "";
		}
	}
 ?>
 <!DOCTYPE html>
<html lang="en">
<head>
  <title>Factorial Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.1/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.1/dist/js/bootstrap.bundle.min.js"></script>
  <style type="text/css">
  	.container{
  		width: 40%;
  	}
  </style>
</head>
<body>
<div class="container mt-3">
  <h2>Factorial program in PHP</h2>
  <form action="" method="post">
    <div class="mb-3 mt-3">
      <label for="email" class="fw-bold">Please enter the number:</label>
      <input type="number" class="form-control" id="email" placeholder="Enter The Number" name="fact_number">
    </div>
    <button type="submit" class="btn btn-primary" name="save">Submit</button>
  </form>
  <br>
  <p style="color: red;" class="fw-bold"><?php echo $factorial_number_err; ?></p>
    <?php
    	if($fact!=""){
    		echo '<p style="color: red;" class="fw-bold">The factorial of the given number is: '.$fact.'</p>';
    	}
    ?>
</div>
</body>
</html>

Conclusion:- I hope this tutorial will help you to understand the concept. If there is any doubt then please leave a comment below.


Leave a Comment

THOR311

ceri188

ceri188

ceri188

KUPU178

HOKI311

RATU311

KUPU178

RATU311

RATU311

kupu178

kupu178

HOKI311

togel

kupu178

ratu311

mix parlay

ceri188

slot online

kupu178

KUPU178

RATU311

THOR311

THOR311

KUPU178

KUPU178

KUPU178

RATU311

RATU311

RATU311

kupu178

hoki311

THOR311

hoki311

togel online

kupu178

slot mahjong

ceri188

slot server thailand

ratu311

ceri188

ceri188

RATU311

punjabi virasat

mimcord.com

Centrum Medyczne

www.vrcorporate.in

phbalance.vn

KUPU178

daftar slot gacor

hoki311

togel hongkong

kupu178

slot mahjong

ratu311

slot gacor

kupu178

kupu178

ceri188

kupu178

ceri188

KUPU178

KUPU178

KUPU178

RATU311

kupu178

RATU311

RATU311

RATU311

KUPU178

KUPU178

RATU311

RATU311

slot online

kupu178

thor311

thor311

kupu178

kupu178

RATU311

KUPU178

THOR311

THOR311

KUPU178

slot mahjong ways

hoki311

slot gacor

ceri188

ratu311

kupu178

Casino Baccarat Online

kupu178

kupu178

ceri188

ceri188

THOR311

THOR311

RATU311

toto macau

KUPU178

slot gacor

HOKI311

sabung ayam online

RATU311

SLOT LUCKY NEKO

CERI188

Slot Gacor

KUPU178

RATU311

RATU311

Link Spaceman

CERI188

Slot Gacor Online

THOR311

Mahjong ways

KUPU178

THOR311

THOR311

Live Casino

HOKI311

CERI188

CERI188

THOR311

THOR311

THOR311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

KUPU178

KUPU178

CERI188

HOKI311 SLOT

TOTO TOGEL TOKYO

CERI188

CERI188 SITE

CERI188

RATU311

RATU311

RATU311

RATU311

RATU311

KUPU178

Toto Togel

KUPU178

Slot Mahjong

CERI188

CERI188

CERI188

Slot Sweet Bonanza 2500

KUPU178

SLOT ONLINE

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

KUPU178

KUPU178 X SLOT777

judi bola online

THOR311

KUPU178 10 GAME SLOT TERBAIK

KUPU178

KUPU178 SLOT TERPERCAYA

KUPU178 TOGEL

TOTO 4D MACAU

KUPU178

SITUS BOLA

CERI188

KUPU178

RATU311

THOR311

HOKI311

KUPU178

RATU311

THOR311

CERI188

Slot Mahjong Ways

KUPU178

CERI188

Slot Online Resmi

KUPU178

RATU311 SLOT ONLINE

KUPU178

KUPU178: Link Akses Resmi

KUPU178: Strategi Menang Di Game Live Casino

KUPU178

KUPU178: Minimal 200 Rupiah

Slot Gacor Online

KUPU178

Mix Parlay

CERI188

CERI188

THOR311

THOR311

THOR311

THOR311

THOR311

KUPU178

Slot Thailand

CERI188

CERI188

THOR311 RTP

RATU311

RATU311

RATU311

CERI188

KUPU178 TOTO SLOT

CERI188 TOTO Singapore

THOR311

KUPU178

KUPU178

RATU311

CERI188

RATU311: Alternatif karnpuracollege

RATU311

RATU311

Thor311 Terbaru

THOR311 MAHJONG

RATU311: BONUS SABUNG AYAM

CERI188: Masuk Akun Slot Super Cepat

THOR311 PRO

CERI188

CERI188

CERI188

THOR311

HOKI311

HOKI311

RATU311

HOKI311

KUPU178

RATU311

RATU311

RATU311

RATU311

THOR311

CERI188

CERI188

RATU311

RATU311

RATU311

RATU311

Game Thor311

HOKI311

HOKI311

RATU311

HOKI311

HOKI311

THOR311

RATU311 NONTON BOLA

RATU311

HOKI311 Link QRIS EWALLET

RATU311

HOKI311 Link Apk Resmi

CERI188

RATU311 judi bola

KUPU178

situs judi bola

HOKI311

RATU311

THOR311

HOKI311

KUPU178

kupu178 link alternatif

kupu178 slot

kupu178 daftar

kupu178 login

link daftar kupu178

judi bola

RATU311

link alternatif RATU311

Link daftar RATU311

link login RATU311

RATU311

KUPU178

KUPU178

RATU311

RATU311

KUPU178

THOR311

slot thailand

judi bola

KUPU178

slot gacor

slot gacor

yakuza303

slot gacor

RATU311

THOR311