First of all, I want to tell you that what is the prime no.
Prime no is the no which can’t be divided by other than 1 and itself.
Prime No? Yes : No;
- <?php
- $no=8;
- $prime=true;
- for($i=2;$i<($no/2);$i++)
- {
- if($no%$i==0){
- $prime=false;
- break;
- }
- }
- if($prime==true){
- echo "<br/>Number is Prime<br/>";
- }
- else{
- echo "<br/>Number is not prime<br/>";
- }
- ?>
There is an unsorted array of integers. write the complete program to find the second largest no in the array without using sorting and max functions. You can only iterate(loop) over the array once.
- <?php
- echo "<br/>";
- echo "Before Sorting<br/>";
- for($i=0;$i<$tot_arr;$i++){
- if(($i< ($tot_arr-1)) && $arr[$i]>$arr[$i+1]){
- $temp=$arr[$i];
- $arr[$i]=$arr[$i+1];
- $arr[$i+1]=$temp;
- $i=-1;
- }
- }
- echo "<br/>After Sorting<br/>";
- ?>
Output:
Before Sorting
Array ( [0] => 12 [1] => 52 [2] => 2 [3] => 35 [4] => 95 [5] => 17 [6] => 37 [7] => 42 )
After Sorting
Array ( [0] => 2 [1] => 12 [2] => 17 [3] => 35 [4] => 37 [5] => 42 [6] => 52 [7] => 95 )