0% found this document useful (0 votes)
93 views27 pages

EX - NO: 1 Date:: Add Up Columns and Rows of A Given

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
93 views27 pages

EX - NO: 1 Date:: Add Up Columns and Rows of A Given

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

EX.

NO: 1
ADD UP COLUMNS AND ROWS OF A GIVEN
DATE : TABLE

AIM :
To write a PHP program to add up the columns and rows of a given table.

ALGORITHM:

Step 1: Start  Select Notepad.


Step 2: Initialize the table matrix in the form of array.
Step 3: Calculates number of rows and columns present in given matrix using count
function
Step 4: Calculates sum of each row and each column of given matrix.
Step 5: Save the php program with .php extension in xampp  htdocs  create a folder called
demo and save the php file in that location.
Step 6: Open the browser and type as /localhost/demo in the address bar. All the programs in
the demo folder are listed
Step 7: Select the respective program name to run
PROGRAM:

<?php
//Initialize matrix a
$a = array
(
array(1, 2, 3),
array(4, 5, 6),
array(7, 8, 9)
);
//Calculates number of rows and columns present in given matrix
$rows = count($a);
$cols = count($a[0]);
echo("Original matrix is:</br>"); // To print Original matrix
for($i = 0; $i < $rows; $i++)
{
for($j = 0; $j < $cols; $j++)
{
echo ($a[$i][$j] . " ");
}
echo("</br>" );
}
echo "Row sum and Column sum of a matrix : "."<br>";
//Calculates sum of each row of given matrix
for($i = 0; $i < $rows; $i++)
{
$sumRow = 0;
for($j = 0; $j < $cols; $j++)
{
$sumRow = $sumRow + $a[$i][$j];
}
echo("Sum of " . ($i+1) ." row: " . $sumRow);
echo("</br>");
}
//Calculates sum of each column of given matrix
for( $i = 0; $i < $cols; $i++)
{
$sumCol = 0;
for($j = 0; $j < $rows; $j++)
{
$sumCol = $sumCol + $a[$j][$i];
}
echo("Sum of " . ($i+1) . " column: " . $sumCol);
echo("</br>");
}
?>
OUTPUT:

Result: Thus the sum of the rows and columns of a given table matrix is calculated
successfully.
EX.NO: 2
SUM OF FIRST N PRIME NUMBERS
DATE :

AIM :
To write a PHP program to compute the sum of first n given prime numbers.

ALGORITHM:

Step 1: Start  Select Notepad.


Step 2: Initialize the sum variable to zero and starts the loop from the number 2 to 100.

Step 4: If the flag ==1 then the loop will print the value of the prime number and sum of
prime numbers.
Step 5: If the falg ==0 then the loop will break
Step 6: Save the php program with .php extension in xampp  htdocs  create a folder called
demo and save the php file in that location
Step 7: Open the browser and type as /localhost/demo in the address bar. All the programs in
the demo folder are listed
Step 8: Select the respective program name to run
PROGRAM:

<?php
$sum=0;
echo("The prime numbers from 2 to 100 are: </br>");
echo("</br>");
for($i=2;$i<=100;$i++)
{
$flag=1;
for($j=2;$j<=$i/2;$j++)
{
if($i%$j==0)
{
$flag=0;
break;
}
}
if($flag==1)
echo "$i ";
{
$sum=$sum+$i;
}
}
echo("</br>");
echo "The sum=$sum";
?>
OUTPUT:

Result: Thus the prime numbers from 2 to 100 is displayed successfully


EX.NO: 3
VALIDATE AN EMAIL ADDRESS
DATE :

AIM :
To write a PHP program to find an email address is valid or not.

ALGORITHM:

Step 1: Start  Notepad


Step 2: Give the email address to validate.

Step 3: Validate the email address using filter_var() function.


Step 4: If email Id is valid display as “Valid Email Address” otherwise displays “Not a Valid
Email address”.
Step 5: Save the php program with .php extension in xampp  htdocs  create a folder called
demo and save the php file in that location
Step 6: Open the browser and type as /localhost/demo in the address bar. All the programs in
the demo folder are listed
Step 7: Select the respective program name to run
PROGRAM:

<?php
// Variable to check
$email = "[email protected]";
// Validate email
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo("$email is a valid email address");
} else {
echo("$email is not a valid email address");
}
?>

Result: Thus the email address is validated successfully


OUTPUT:
EX.NO: 4
NUMBER INTO WORDS
DATE :

AIM :
To Write a PHP program to convert a number that to be written in words.

ALGORITHM:

Step 1: Start  Notepad


Step 2: Declare the arrays for necessary variables.

Step 3: if the number is < 10 display as from units array.


Step 4: if the number < 20 display as from special array.
Step 5: Otherwise the number is divided by 10 display as from units and tens array.
Step 6: For the sample input 60, SIXTY in words will be displayed.
Step 7: Save the php program with .php extension in xampp  htdocs  create a folder called
demo and save the php file in that location.
Step 8: Open the browser and type as /localhost/demo in the address bar. All the programs in
the demo folder are listed.
Step 9: Select the respective program name to run.
PROGRAM:

<?php
function numToWords($number)
{
$units = array('', 'one', 'two', 'three', 'four','five', 'six', 'seven', 'eight',
'nine');

$tens = array('', 'ten', 'twenty', 'thirty', 'forty','fifty', 'sixty', 'seventy',


'eighty','ninety');

$special = array('eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen',


'seventeen', 'eighteen', 'nineteen');

$words = '';
if ($number < 10)
{
$words .= $units[$number];
}
elseif ($number < 20)
{
$words .= $special[$number - 11];
}
else
{
$words .= $tens[(int)($number / 10)] . ' '
. $units[$number % 10];
}
return $words;
}
// Example usage:
$number = 60;
echo "Number $number in words: "
. numToWords($number);

?>
OUTPUT:

Result: Thus the number is converted into words successfully


EX.NO: 5 DELAY PROGRAM EXECUTION
DATE :

AIM :
To write a PHP script to delay the program execution for the given number of seconds.

ALGORITHM:

Step 1: Start  Notepad


Step 2: display the system’s current time in 'hour:minute:second' format using date function
Step 3: Sleep(5) function used to delay the output.
Step 4: Save the php program with .php extension in xampp  htdocs  create a folder called
demo and save the php file in that location
Step 5: Open the browser and type as /localhost/demo in the address bar. All the programs in
the demo folder are listed
Step 6: Select the respective program name to run
PROGRAM:

<?php
// Output the current time in the 'hour:minute:second' format
echo date(' h : i : s ') . "\n";

// Sleep for 5 seconds


sleep(5);

// Output the current time again after waking up


echo date(' h : i :s ')."\n";
?>
OUTPUT:

Result: Thus the program execution is delayed for 5 seconds successfully.


EX.NO: 6
CHANGE THE COLOR OF THE FIRST
DATE : CHARACTER

AIM :
To write a PHP script, which changes the color of the first character of a word

ALGORITHM:

Step 1: Start  Notepad


Step 2: Initialize the string to variable.

Step 3: Use the preg_replace () function to change the first letter of the text PHPtutorial for
all upper case and lower case letters to red color.
Step 4: Display the resulted text
Step 5: Save the php program with .php extension in xampp  htdocs  create a folder called
demo and save the php file in that location
Step 6: Open the browser and type as /localhost/demo in the address bar. All the programs in
the demo folder are listed
Step 7: Select the respective program name to run
PROGRAM:
<?php
$text="PHPtutorial";
$text=preg_replace('/(\b[a-z])/i','<span style="color:red">\1</span>',
$text);
echo $text;
?>
OUTPUT:

Result: Thus the color of the first letter of the word is changed successfully.
EX.NO: 7
MULTIPLICATION TABLE
DATE :

AIM :
To write a PHP program to find multiplication table of a number.

ALGORITHM:

Step 1: Start  Notepad


Step 2: Initialize the number(For example , 7) to generate the multiplication table

Step 3: Use the for loop to display the multiplication table of the initialized
Number from 1 to 10
Step 5: Save the php program with .php extension in xampp  htdocs  create a folder
called demo and save the php file in that location
Step 6: Open the browser and type as /localhost/demo in the address bar. All the programs
in the demo folder are listed
Step 7: Select the respective program name to run
PROGRAM:

<?php
$num= 7;
echo "Multiplication table of ", $num, " is";
echo "<br>";
for($i=1; $i<=10; $i++)
{
echo "\t", $i, "x", $num, "=", $i * $num, "<br>";
}
?>
OUTPUT:

Result: Thus the multiplication table of 7 will be displayed successfully.


EX.NO: 8
FACTORIAL OF A NUMBER
DATE :

AIM :
To write a PHP program to calculate Factorial of a number.

ALGORITHM:

Step 1: Start  Notepad


Step 2: Initialize the number to 5 (i.e., to generate the factorial value of 5)
Step 3: Initialize the value of the factorial variable and i to 1
Step 4: Use the $factorial=$factorial*$i to calculate the factorial value of 5 (5*4*3*2*1).
Step 5: Save the php program with .php extension in xampp  htdocs  create a folder
called demo and save the php file in that location
Step 6: Open the browser and type as /localhost/demo in the address bar. All the programs
in the demo folder are listed
Step 7: Select the respective program name to run
PROGRAM:

<?php
function Factorial($number)
{
$factorial= 1;
for($i=1; $i <= $number; $i++)
{
$factorial=$factorial*$i;
}
return $factorial;
}
$number = 5;
$fact = Factorial($number);
echo "Factorial = $fact";
?>
OUTPUT:

Result: Thus the factorial value of a number calculated successfully.

You might also like