Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
17 views

PHP Programs

The document contains code snippets for performing arithmetic operations, determining if a number is even or odd, calculating factorials, determining if a number is prime, and calculating the sum of numbers. The code snippets demonstrate using forms and PHP to perform calculations and return results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

PHP Programs

The document contains code snippets for performing arithmetic operations, determining if a number is even or odd, calculating factorials, determining if a number is prime, and calculating the sum of numbers. The code snippets demonstrate using forms and PHP to perform calculations and return results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

addition of two

<html>
<body>
<form method="post">
Enter First Number:
<input type="number" name="number1" /><br><br>
Enter Second Number:
<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>

oddoreven

<html>
<body>
<form method="post">
Enter a number:
<input type="number" name="number">
<input type="submit" value="Submit">
</form>
</body>
</html>
<?php
if($_POST){
$number = $_POST['number'];
if(($number % 2) == 0){
echo "$number is an Even number";
}else{
echo "$number is Odd number";
}
}
?>
arithmetic operations

<html>
<body>
<form method="post">
enter no 1: <input type="text" name=t1 value=""><br><br>
enter no 2: <input type="text" name=t2 value=""><br><br>
<input type="submit" name="s1" value="add">
<input type="submit" name="s2" value="sub">
<input type="submit" name="s3" value="mul">
<input type="submit" name="s4" value="div">
</form>
<?php

if(isset($_POST['t1']))
{
$n1=$_POST['t1'];
}
if(isset($_POST['t2']))
{
$n2=$_POST['t2'];
}
if(isset($_POST['s1']))
{
$n3 = $n1+ $n2;
}
if(isset($_POST['s2']))
{
$n3 = $n1-$n2;
}
if(isset($_POST['s3']))
{
$n3 = $n1* $n2;
}
if(isset($_POST['s4']))
{
$n3 = $n1/$n2;
}
echo $n3;
?>
</body>
</html>

sum of n numbers

<html>
<head> SUM OF N NUMBERS <br> </head>
<body>
<form method="post">
Enter N Number to Generate Sum of 'n' numbers :
<input type="number" name="num"/><br><br>
<input type="submit" name="Submit" value="Submit">
</form>
<?php
if(isset($_POST['Submit']))
{
$n = $_POST['num'];
$sum = 0;
for($i = 1; $i <= $n; ++$i)
{
$sum = $sum + $i;
}
echo "Sum of $n numbers = $sum";
}
?>
</body>
</html>

factorial

<html>
<head>
<title>Factorial Program </title>
</head>
<body>
<form method="post">
Enter the Number:<br>
<input type="number" name="number" id="number">
<input type="submit" name="submit" value="Submit" />
</form>
<?php
if($_POST){
$fact = 1;
//getting value from input text box 'number'
$number = $_POST['number'];
echo "Factorial of $number:<br><br>";
//start loop
for ($i = 1; $i <= $number; $i++){
$fact = $fact * $i;
}
echo $fact . "<br>";
}
?>
</body>
</html>

prime number

<html>
<body>
<form method="post">
Enter a Number: <input type="text" name="input"><br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
if($_POST)
{
$input=$_POST['input'];
for ($i = 2; $i <= $input-1; $i++) {
if ($input % $i == 0) {
$value= True;
}
}
if (isset($value) && $value) {
echo 'The Number '. $input . ' is not prime';
} else {
echo 'The Number '. $input . ' is prime';
}
}
?>

</body>
</html>

You might also like