EX - NO: 1 Date:: Add Up Columns and Rows of A Given
EX - NO: 1 Date:: Add Up Columns and Rows of A Given
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:
<?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 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:
AIM :
To write a PHP program to find an email address is valid or not.
ALGORITHM:
<?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");
}
?>
AIM :
To Write a PHP program to convert a number that to be written in words.
ALGORITHM:
<?php
function numToWords($number)
{
$units = array('', 'one', 'two', 'three', 'four','five', 'six', 'seven', 'eight',
'nine');
$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:
AIM :
To write a PHP script to delay the program execution for the given number of seconds.
ALGORITHM:
<?php
// Output the current time in the 'hour:minute:second' format
echo date(' h : i : s ') . "\n";
AIM :
To write a PHP script, which changes the color of the first character of a word
ALGORITHM:
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 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:
AIM :
To write a PHP program to calculate Factorial of a number.
ALGORITHM:
<?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: