Author: Codereplica
An array is a special variable, which can hold more than one value at a time.Let’s see the code example for merging two array in third empty array without using PHP in-built function. <?php $array_a = ['1','2','3']; $array_b = ['4','5','6']; $array_c = array(); for($i=0;$i<count($array_a); $i++ ){ $array_c[$i] = $array_a[$i]; } $count = count($array_a); for($i=($count),$j=0;$i<count($array_b)+$count; $i++,$j++){ $array_c[$i] = $array_b[$j]; } print_r($array_c); Output : Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 )
Floyd’s triangle is a right-angled triangular array of natural numbers.It is named after Robert Floyd. It is defined by filling the rows of the triangle with consecutive numbers, starting with a 1 in the top left corner.Now let’s see the code example: <?php $a =1; for($i=0;$i<=4;$i++) { for($j=1;$j<=$i;$j++) { echo $a." "; $a++; } echo "<br>"; } Output : 1 2 3 4 5 6 7 8 9 10
Fibonacci series are number series,in which next number is the sum of previous two numbers.Example: 0, 1, 1, 2, 3, 5, 8, 13, 21 etc Let’s try to get 5 Fibonacci number series – <?php $a = 1; $b = 1; echo $a." ".$b." "; for($i=1; $i<=5; $i++) { $c = $a+$b; echo $c." "; $temp = $b; $b = $c; $a = $temp; } Output : 1 1 2 3 5
An armstrong number is a number that is equal to the sum of cubes of its digits.For example 0, 1, 153, 370, 371 and 407 are the Armstrong numbers. Let’s see the approach ,how to find given number is armstrong or not ? <?php $num = 407; // user input $sum =0; $temp = $num; while($temp !=0) { $rem = $temp % 10; $sum = $sum+$rem*$rem*$rem; $temp = $temp/10; } if($num == $sum) { echo $num." is armstrong"; } else{ echo $num." is not a armstrong"; } Output: 407 is armstrong
There are different pattern design available in PHP using asterisk (*) symbol.Such as Diamond Pattern, Diamond Triangle number,Reverse Pyramid Pattern,Heart Star Pattern,half star pyramid pattern, Pyramid,alphabet pattern.Here i show you few of them. Now let’s first see the half diamond pattern in descending order- <?php for($i=0;$i<=5;$i++) { for($j=5-$i;$j>=1;$j–) { echo "* "; } echo "<br>"; } Output : * * * * * * * * * * * * * * * Half diamond pattern in ascending order- <?php for($i=0;$i<=5;$i++) { for($j=1;$j<=$i;$j++) { echo "* "; } echo "<br>"; } Output: * * * * * * * *…
A palindrome is a word, number, phrase, or other sequence of characters which reads the same backward as forward, such as madam, racecar, 11, 22, 131, 121, etc. Let’s find palindrome using PHP in-built function <?php $n = 'madam'; // User input either string or number. Ex: 131, 121, level if($n == strrev($n)){ echo "This is palindrome."; } else{ echo "Not a palindrome."; } Output : This is palindrome. Explanation : Here i’m using “strrev” PHP in-built function which reverse the given input string or number. Now, let’s find palindrome without using PHP in-built function <?php $mystring = 'madam'; //…
In PHP we can swap two numbers in two ways-1. Swap two numbers using third variable.2. Swap two numbers using without third variable. Let’s see 1st example of swapping of two numbers using third variable. <?php $a = 15; $b = 10; echo "Before swap :<br>"; echo "a : ".$a; echo "<br>"; echo "b :".$b; echo "<br>"; $temp = $a; $a = $b; $b= $temp; echo "After swap :<br>"; echo "a : ".$a; echo "<br>"; echo "b :".$b; Output : Before swap : a : 15 b : 10 After swap : a : 10 b : 15 Now let’s…
LCM stands for “Least Common Multiple”.The least common multiple (LCM) of two numbers is the smallest number (not zero) that is a multiple of both.Let’s see the example -Q. Find LCM of 3 and 4A. First check multiply of both numbers.Multiply of 3 are :0,3,6,9,12,15,18,21,24…..Multiply of 4 are :0,4,8,12,16,20,24….. In the above common multiply of 3 and 4 are – 0,12,24…But as i already tell LCM of two numbers is the smallest number (not zero) that is a multiple of both,so we except 0 from above result then we get LCM of 3 and 4 is 12. Now solve this…
In mathematics, the factorial function (symbol: !) says to multiply all whole numbers from our chosen number down to 1.Example : Factorial of 5 is -5! = 5 x 4 x 3 x 2 x 1 = 120 Let’s achieve this with PHP – <?php $fact =1; $n = 5; // user input for($i=1; $i<=$n; $i++) { $fact = $fact * $i; } echo $fact; // Output // 120
An array is a special variable, which can hold more than one value at a time.Task: To merge two array on third empty array and sort third array in ascending order without using inbuilt functionSolution Steps:1. Take 2 sample array and store them in a varaible.2. Take one empty array.3. Merge the first 2 array and store them in empty array.4. Now sort the new array in ascending order without using PHP in-built function. Now let’s see the code – <?php // Sets up our arrays $array1 = [1, 30, 5, 7, 9,20,25,3]; $array2 = [2, 4, 6, 8, 10];…