PHP WEB DEVELOPMENT INTERVIEW CODING PREPARED BY ME OF PITANGENT ANALYTICS AND TECHNOLOGY SOLUTIONS PVT. LTD FOR FRESHERS
PHP WEB DEVELOPMENT INTERVIEW CODING PREPARED BY ME OF PITANGENT ANALYTICS AND TECHNOLOGY SOLUTIONS PVT. LTD FOR FRESHERS
Q1. Write a PHP program to count the number of vowels in a string.
Ans: PROGRAM NO. - 1 =>
<?php
$str="Welcome To Capgemini";
$vow=0;
$str=str_replace(" ", "", $str);
$len=strlen($str);
for ($j=0; $j < $len; $j++) {
if ($str[$j] == "a" || $str[$j] == "e" || $str[$j] == "i" || $str[$j] == "o" || $str[$j] == "u" || $str[$j] == "A" || $str[$j] == "E" || $str[$j] == "I" || $str[$j] == "O" || $str[$j] == "U") {
$vow++;
}
}
print_r("The number of vowels in a string is: ".$vow);
?>
OUTPUT OF PROGRAM NO. - 1 =>
PROGRAM NO. - 2 =>
<?php
$str="Welcome To Pitangent";
$vow=0;
$str=str_replace(" ", "", $str);
$length=strlen($str);
for ($i=0; $i < $length; $i++) {
if ($str[$i] == 'a' || $str[$i] == 'e' || $str[$i] == 'i' || $str[$i] == 'o' || $str[$i] == 'u' || $str[$i] == 'A' || $str[$i] == 'E' || $str[$i] == 'I' || $str[$i] == 'O' || $str[$i] == 'U') {
$vow++;
}
}
print_r("The number of vowels in a string is: ".$vow."<br>");
?>
OUTPUT OF PROGRAM NO. - 2 =>
Q2. Write a PHP code to check whether a number is prime or not.
Ans: PROGRAM NO. - 1 =>
<?php
function isPrime($num){
if ($num <= 1) {
return false;
}
for ($i=2; $i <= sqrt($num); $i++) {
if ($num%$i==0) {
return false;
}
}
return true;
}
$number=19;
if (isPrime($number)) {
echo "$number is a prime number"."<br>";
}
else{
echo "$number is not a prime number";
}
?>
Comments
Post a Comment