You are on page 1of 29

PRACTICAL FILE

ON
WEB BASED PROGRAMMING
                   BCA 2st semester
               Course code: BCA-
Session: 2022-23

SUBMITTED TO:                      SUBMITTED BY:  


  
DR.SUMIT CHAUHAN Name: RONIT
THAKUR
INDEX
S.NO. TOPIC SIGN
1. Write a PHP program to print sum of
digits.
2. Write a PHP program to check prime
number.
3. Write a PHP program to check even or odd
number.
4. Write a PHP program to print table of a
number.
5. Write a PHP program to print factorial of a
number.
6. Write a PHP program to reverse given
number.
7. Write a PHP program to reverse given
string.
8. Write a PHP program to swap two
numbers with and without using third
variable.
9. Write a PHP program to add, subtract,
multiply & divide two numbers using
functions.
10. Write a program to show the usage of
SESSION variable.
11. Write a program to show the usage of
$_POST , $_GET and $_REQUEST
variables.
12. Write a login form and validate it through
PHP programming.
PROGRAM-1
Write a PHP program to print sum of digits.
<html>

<head></head>

<body>

<form method='POST' action="<?php echo $_SERVER['PHP_SELF'] ?>">

SUM OF NUMBERS<br>

ENTER FIRST NUMBER <input type="text" name="num1"><BR>

ENTER SECOND NUMBER<input type="text" name="num2"><BR>

<input type="submit" value="add">

</form>

</html>

<?php

$no1=$_POST['num1'];

$no2=$_POST['num2'];

$a=$no1+$no2;

echo"sum of $no1 and $no2 = $a";

?>
OUTPUT:
PROGRAM-2
Write a PHP program to check prime number.
<html>

<head></head>

<body>

TO CHECK PRIME OR NOT<br>

<form method='POST'action="<?php echo $_SERVER['PHP_SELF']?>">

ENTER THE NUMBER<INPUT type="text" name="n1">

<input type="submit" value="CLICK">

</form>

<?php

$n=$_POST['n1'];

$count=0;

for ( $i=1; $i<=$n; $i++) {

if (($n%$i)==0) {

$count++;

} }

if ($count<3) {

echo "YES,$n is a prime number.";

else{

echo"NO,$n is not a prime number.";

?>
OUTPUT:
PROGRAM-3
Write a PHP program to check even or odd number.
<html>

<head></head>

<body>

TO CHECK ODD OR EVEN<br>

<form method='POST'action="<?php echo $_SERVER['PHP_SELF']?>">

ENTER<INPUT type="text" name="n1">

<input type="submit" value="check">

</form>

<?php

$n=$_POST['n1'];

if($n%2==0)

echo"$n is EVEN NUMBER";

else {

echo"$n is ODD NUMBER";

?>
OUTPUT:
PROGRAM-4
Write a PHP program to print table of a number.
<html>

<head></head>

<body>

TABLE

<form method='POST'action="<?php echo $_SERVER['PHP_SELF']?>">

<br>ENTER NUMBER<INPUT type="text" name="n1">

<input type="submit" value="check">

</form>

<?php

$n=$_POST['n1'];

for($i=1;$i<=10;$i++)

$c=$i*$n;

echo"$n x $i= $c <BR>";

?>
OUTPUT:
PROGRAM-5
Write a PHP program to print factorial of a number.
<html>

<head></head>

<body>

FACTORIAL CALCULATOR

<form method='POST'action="<?php echo $_SERVER['PHP_SELF']?>">

<br>ENTER<INPUT type="text" name="n1">

<input type="submit" value="FIND">

</form>

<?php

$n=$_POST['n1'];

$factorial=1;

for ($i=$n; $i>=1; $i--)

$factorial = $factorial * $i;

echo "Factorial of $n is $factorial";

?>
OUTPUT:
PROGRAM-6
Write a PHP program to reverse given number.
<html>

<head></head>

<body>

TO REVERSE NUMBER

<form method='POST'action="<?php echo $_SERVER['PHP_SELF']?>">

<br>ENTER<INPUT type="text" name="n1">

<input type="submit" value="REVERSE">

</form>

<?php

$n=$_POST['n1'];

$revnum = 0;

while ($n > 1)

$rem = $n % 10;

$revnum = ($revnum * 10) + $rem;

$n = ($n / 10);

echo "Reverse = $revnum";

?>
OUTPUT:
PROGRAM-7
Write a PHP program to reverse given string.
<html>

<head></head>

<body>

TO REVERSE THE STRING

<form method='POST'action="<?php echo $_SERVER['PHP_SELF']?>">

<br>ENTER<INPUT type="text" name="string">

<input type="submit" value="reverse">

</form>

<?php

$str=$_POST['string'];

$a=strrev("$str");

echo "$a";

?>
OUTPUT:
PROGRAM-8
Write a PHP program to swap two numbers with and without using
third variable.
SWAPPING WITH THIRD VARIABLE

<html>

<head></head>

<body>

Swapping of two numbers with third variable

<form method='POST'action="<?php echo $_SERVER['PHP_SELF']?>">

<br>Enter the first no <INPUT type="text" name="num1"><br>

Enter the second no <INPUT type="text" name="num2">

<br><input type="submit" value="Swap">

</form>

<?php

$a =$_POST['num1'];

$b = $_POST['num2'];

$third = $a;

$a = $b;

$b = $third;

echo "After swapping:<br><br>";

echo "First=".$a."

Second=".$b;

?>
SWAPPING WITHOUT THIRD VARIABLE

<html>

<head></head>

<body>

Swapping of two numbers without third variable

<form method='POST'action="<?php echo $_SERVER['PHP_SELF']?>">

<br>Enter the first no <INPUT type="text" name="num1"><br>

Enter the second no <INPUT type="text" name="num2">

<br><input type="submit" value="Swap">

</form>

<?php

$a=$_POST['num1'];

$b=$_POST['num2'];

$a=$a+$b;

$b=$a-$b;

$a=$a-$b;

echo"After swapping<br>";

echo "Value of a: $a<br>";

echo "Value of b: $b<br>";

?>
OUTPUT:
PROGRAM-9
Write a PHP program to add, subtract, multiply & divide two
numbers using functions.
<html>

<head></head>

<body>

ARITHMATIC CALCULATOR<BR><BR>

<form method='POST' action="<?php echo $_SERVER['PHP_SELF'] ?>">

ENTER NO 1 <input type="text" name="num1"><BR><BR>

ENTER NO 2 <input type="text" name="num2"><BR><BR>

<input type="submit" value="CALCUATE">

</form>

</html>

<?php

function add()

$no1=$_POST['num1'];

$no2=$_POST['num2'];

$c=$no1+$no2;

echo"SUM of $no1 and $no2 = $c <br>";

add();

function sub()

$no1=$_POST['num1'];
$no2=$_POST['num2'];

$c=$no1-$no2;

echo"SUBTRACTION of $no1 and $no2 = $c <br>";

sub();

function mul()

$no1=$_POST['num1'];

$no2=$_POST['num2'];

$c=$no1*$no2;

echo"MULTIPLICATION of $no1 and $no2 = $c <br>";

mul();

function div()

$no1=$_POST['num1'];

$no2=$_POST['num2'];

$c=$no1%$no2;

echo"DIVISION of $no1 and $no2 = $c <br>";

div();

?>
OUTPUT:
PROGRAM-10
Write a program to show the usage of SESSION variable.
<?php

session_start();

session_destroy();

?>

<html>

<body>

<?php

$_SESSION["NAME"] = "RONIT";

$_SESSION["AGE"] = "19";

?>

<?php

echo ' NAME of student :' . $_SESSION["NAME"] . '<br>';

echo ' AGE of student :' . $_SESSION["AGE"] . '<br>';

?>

<?php

if(isset($_SESSION["NAME"])){

unset($_SESSION["AGE"]);

?>

</body>

</html>
OUTPUT:
PROGRAM-11
Write a program to show the usage of $_POST , $_GET and
$_REQUEST variables.
Using REQUEST for the method

<form action="method.php" method="REQUEST">

Name:<br><input type="text" name="name"><br>

<input type="submit" value="Submit">

</form>

Using GET for the method

<form action="method.php" method="GET">

Name:<br><input type="text" name="name"><br>

<input type="submit" value="Submit">

</form>

Using POST for the method

<form action="method.php" method="POST">

Name:<br><input type="text" name="name"><br>

<input type="submit" value="Submit">

</form>
OUTPUT:
PROGRAM-12
Write a login form and validate it through PHP programming.
<html>

<head></head>

<body>

<form method='POST' action="<?php echo $_SERVER['PHP_SELF'] ?>">

USERID: <input type="text" name="user"><BR>

PASSWORD: <input type="text" name="pass"><BR><br>

<input type="submit" value="LOGIN">

</form>

</html>

<?php

$user1=$_POST['user'];

$pass1=$_POST['pass'];

if($user1=='RONIT' && $pass1=112233 )

echo "LOGIN SUCCESSFUL";

else

echo "INCORRECT USERID OR PASSWORD";

?>
OUTPUT:

You might also like