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

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

KUPU178

KUPU178

KUPU178

hoki311

hoki311

hoki311

hoki311

hoki311

RATU311

KUPU178

HOKI311

THOR311

THOR311

login hoki311

daftar hoki311

hoki 311

link hoki311

hoki311

RATU311

hoki311

hoki311

hoki311

hoki311

KUPU178

thor311

thor311

thor311

ceri188

ceri188

KUPU178

ceri188

ceri188

link ceri188

KUPU178

KUPU178

KUPU178

kupu178

ceri188

ceri188

ceri188

ceri188

ceri188

THOR311

ceri188

games online ceri188

link login ceri188

ceri188

thor311

RATU311

RATU311

RATU311

sbobet88

sbobet

parlay

mix parlay

judi bola online

thor311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

THOR311

THOR311

THOR311

THOR311

thor311 RTP

thor311

slot gacor

kupu178

slot mahjong

ceri188

sabung ayam

ratu311

THOR311

THOR311

THOR311

thor311 aplikasi

ratu311 sabung ayam

slot gacor

RATU311

ceri188

ceri188

ceri188

ceri188

KUPU178

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

ceri188

ceri188

ceri188 alternatif

ceri188

link ceri188

ceri188

RATU311

THOR311

THOR311

THOR311

RATU311

RATU311

RATU311

Togel Online

KUPU178

Situs Togel

KUPU178

ceri188

ceri188

KUPU178

KUPU178

ceri188

ceri188

ratu311

ceri188

ceri188

ratu311

KUPU178

THOR311

ceri188

ceri188

THOR311

THOR311

RATU311

thor311 tajen bali

thor311 alternatif

situs toto4d

thor311 toto4d

thor311 akses

ceri188

daftar ceri188

cery188

ceri188

slot gacor

kupu 178

kupu178

ceri1888

ceri188

slot online

hoki311

togel online

ceri188

ding dong

ratu311

slot gacor

kupu178

live casino

kupu178

mix parlay

ceri188

judi bola online

thor311 slot

THOR311

RATU311

KUPU178

THOR311

ceri188

ratu311

slot online

hoki311

kupu178

togel online

kupu178

slot gacor

ceri188

judi bola online

ceri188

ratu311

kupu178

RATU311

RATU311

live casino roullete

ceri188

slot pg soft

kupu178

KUPU178

THOR311

RATU311

KUPU178

THOR311

THOR311

thor311 domino qq

thor311 akses

thor dingdong

ceri188

ceri188

kupu178

kupu178

togel

kupu178

mix parlay

ceri188

slot online

kupu178

THOR311

THOR311

KUPU178

kupu178

hoki311

togel online

kupu178

ceri188

ceri188

www.vrcorporate.in

phbalance.vn

togel hongkong

kupu178

slot gacor

kupu178

kupu178

ceri188

kupu178

KUPU178

RATU311

slot online

kupu178

THOR311

KUPU178

slot mahjong ways

hoki311

slot gacor

ceri188

kupu178

Casino Baccarat Online

kupu178

kupu178

ceri188

toto macau

KUPU178

Slot Gacor

KUPU178

RATU311

Mahjong ways

KUPU178

KUPU178

KUPU178

RATU311

RATU311

Toto Togel

KUPU178

CERI188

Slot Sweet Bonanza 2500

KUPU178

RATU311

KUPU178 X SLOT777

judi bola online

THOR311

KUPU178 SLOT TERPERCAYA

KUPU178 TOGEL

TOTO 4D MACAU

KUPU178

THOR311

HOKI311

KUPU178

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

THOR311

THOR311

THOR311

THOR311

THOR311

KUPU178

Slot Thailand

CERI188

CERI188

THOR311 RTP

RATU311

CERI188

KUPU178 TOTO SLOT

CERI188 TOTO Singapore

THOR311

CERI188

RATU311: Alternatif karnpuracollege

Thor311 Terbaru

THOR311 MAHJONG

RATU311: BONUS SABUNG AYAM

CERI188: Masuk Akun Slot Super Cepat

THOR311

HOKI311

HOKI311

RATU311

HOKI311

KUPU178

RATU311

RATU311

THOR311

CERI188

CERI188

RATU311

RATU311

RATU311

Game Thor311

HOKI311

HOKI311

RATU311

HOKI311

HOKI311

RATU311 NONTON BOLA

HOKI311 Link QRIS EWALLET

RATU311

HOKI311 Link Apk Resmi

RATU311 judi bola

KUPU178

HOKI311

RATU311

THOR311

HOKI311

KUPU178

KUPU178

RATU311

RATU311

KUPU178

THOR311

KUPU178

RATU311

THOR311