Hi There,
PHP Program is all about logic.In this article, we’ll go through examples of logical programs in php. These programs are frequently asked in the interview. So I have shared a few of the logical programs. If you want to ask or share any new questions write it in the comment section.
1.Sort an array without using any sorting function in PHP
1.a) Example $array = array(98,76,100,32,1,4,75); make this array ascending order without using any inbuilt function.
Answer:
<?php
$array = array(98,76,100,32,1,4,75);
echo '<pre>';
echo "Before Sorting Array: ";
print_r($array);
$total = count($array);
for ($i=0; $i < $total; $i++) {
for ($j=$i+1; $j < $total; $j++) {
if($array[$i] > $array[$j]) {
$temp = $array[$i];
$array[$i] = $array[$j];
$array[$j] = $temp;
}
}
}
echo '<pre>';
echo "Ascending Sorted Array is: ";
print_r($array);
?>
Output:
Before Sorting Array: Array
(
[0] => 98
[1] => 76
[2] => 100
[3] => 32
[4] => 1
[5] => 4
[6] => 75
)
Ascending Sorted Array is: Array
(
[0] => 1
[1] => 4
[2] => 32
[3] => 75
[4] => 76
[5] => 98
[6] => 100
)
1.b)Example $array = array(98,76,100,32,1,4,75); make this array descending order without using any inbuilt function.
Answer:
<?php
$array = array(98,76,100,32,1,4,75);
echo '<pre>';
echo "Before Sorting Array: ";
print_r($array);
$total = count($array);
for ($i=0; $i < $total; $i++) {
for ($j=$i+1; $j < $total; $j++) {
if($array[$i] < $array[$j]) {
$temp = $array[$i];
$array[$i] = $array[$j];
$array[$j] = $temp;
}
}
}
echo '<pre>';
echo "Ascending Sorted Array is: ";
print_r($array);
?>
Output:
Before Sorting Array: Array
(
[0] => 98
[1] => 76
[2] => 100
[3] => 32
[4] => 1
[5] => 4
[6] => 75
)
Ascending Sorted Array is: Array
(
[0] => 100
[1] => 98
[2] => 76
[3] => 75
[4] => 32
[5] => 4
[6] => 1
)
2.Find the smallest number in an array without using any function.
a) Find the minimum array value $array = array(98,76,100,32,1,4,75);
<?php
$array = array(98,76,100,32,1,4,75);
$count = count($array);
$min = $array[0];
for ($i=0; $i < $count; $i++) {
if($array[$i] < $min)
{
$min = $array[$i];
}
}
echo "smallest value $min";
?>
3.Find the largest number in an array without using any function.
a) Find the minimum array value $array = array(98,76,100,32,1,4,75);
<?php
$array = array(98,76,100,32,1,4,75);
$count = count($array);
$max = $array[0];
for ($i=0; $i < $count; $i++) {
if($array[$i] > $max)
{
$max = $array[$i];
}
}
echo "Largest value $max";
?>
With PHP function to find min min() and max max() array value.
<?php
$array = array(98,76,100,32,1,4,75);
$min = min($array);
$max = max($array);
echo 'Smallest Number: '.$min.'<br>';
echo 'Largest Number: '.$max.'<br>';
?>
4. How to get highest and second highest from single line code?
<?php
$number = array(98,76,100,32,1,4,75);
sort($number);
$highest = $number[count($number)-1];
echo 'Highest Number';
print_r($highest);
$secondHighest = $number[count($number)-2];
echo 'Second Highest Number';
print_r($secondHighest);
5.How to find Second Highest number from array without inbuilt array functions?
<?php
function findSecondMax(array $arr) {
//If array is empty then return
if(empty($arr)) {
return;
}
/*
* Initialize max and second max with negative value
*/
$max = -1;
$secondMax = -1;
//Traverse an array
foreach($arr as $number) {
//If it's greater than the value of max
if($number > $max) {
$secondMax = $max;
$max = $number;
}
//If array number is greater than secondMax and less than max
if($number > $secondMax && $number < $max) {
$secondMax = $number;
}
}
return $secondMax;
}
$arr = array(98,76,100,32,1,4,75,100);
$second_maximum = findSecondMax($arr);
echo "Second Highest Element is ".$second_maximum;
6. Write a Function that combines two lists by alternatingly taking element. For example Given two lists [k,l,o] and [1, 2, 3] the function should return [k, 1, l, 2, o, 3]
<?php
$arr = array(1,2,3);
$arr1 = array('k','l','o');
$new_array = array();
for ($i=0; $i < count($arr); $i++)
{
$new_array[] = $arr1[$i];
$new_array[] = $arr[$i];
}
echo '<pre>'; print_r($new_array);
?>
7.Remove duplicates from an array without using any inbuilt functions?
<?php
$number = array(98,76,100,32,1,4,75,76,76,76,100,100,98,98,32,32);
$newArr = array();
foreach($number as $val){
$newArr[$val] = $val;
}
echo '<pre>'; print_r($newArr);
?>
8.Find the prime numbers 1 to 100
<?php
$number = 2;
while ($number < 100)
{
$divid_number=0;
for ($i=1;$i<=$number;$i++)
{
if (($number%$i)==0)
{
$divid_number++;
}
}
if ($divid_number < 3)
{
echo $number.'<br>';
}
$number = $number+1;
}
?>
9.Write a Program for factorial number.
The factorial generates subsequent number by adding two previous numbers.
factorial of number n = n! = n(n-1)(n-2)...1
<?php
if(isset($_POST['submit']))
{
$num=$_POST['num'];
$fact = 1;
for ($i=$num; $i>=1; $i--)
{
$fact = $fact * $i;
}
echo "factorial of $num is $fact";
}
?>
10. Swapping two numbers using temp
<?php
$x = 12;
$y = 10;
echo 'Before swapping: x='.$x.'y='.$y;
$temp = $x;
$x = $y;
$y = $temp;
echo '<br>';
echo '<br>';
echo 'after swapping: x='.$x.'y='.$y;
?>
11.Swapping two numbers without using temp
<?php
function swap($x, $y) {
echo "Before Swap.\n";
echo "x = $x \n";
echo "y = $y \n";
//Swap technique
$x = $x + $y;
$y = $x - $y;
$x = $x - $y;
echo "After Swap.\n";
echo "x = $x \n";
echo "y = $y \n";
}
swap(12, 12);
?>
12. Reverse String Program without inbuilt function
<?php
$string= 'praba';
$length = strlen($string);
for ($i=($length-1) ; $i >= 0 ; $i--)
{
echo $string[$i];
}
13.Check whether a year is a Leap Year or not
<?php
$year = 2022;
if($year % 4 == 0) {
echo "This is a leap year";
}
else {
echo "This is a not leap year";
}
?>
14.Find odd and even numbers
<?php
$number = 111;
if($number %2 == 0) {
echo "$number is Even number <br/>";
} else {
echo "$number is Odd number <br/>";
}
?>
15. Find Fibonacci series for the given number 12
<?php
$n = 12;
$a = -1;
$b = 1;
for($i = 1; $i <= $n; $i++) {
$c = $a + $b;
echo $c." ";
$a = $b;
$b = $c;
}
?>
Comments
Post a Comment