-
Notifications
You must be signed in to change notification settings - Fork 0
/
que2.php
120 lines (93 loc) · 2.18 KB
/
que2.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<!-- Write a php program to:
a) check whether given number is palindrome or not.
b) print sum of digits.
c) print factorial of a number.
d) display text messages.
e) print an array.
f) create a simple calculator
g) find vowel checking a string. -->
<?php
// a) check whether given number is palindrome or not.
function palindrome($n){
$temp = $n;
$rev = 0;
while(floor($temp)){
$d = $temp % 10;
$rev = $rev * 10 + $d;
$temp = $temp / 10;
}
if($rev == $n){
echo "Palindrome<br>";
}
else{
echo "Not Palindrome<br>";
}
}
$n = 12321;
palindrome($n);
// b) print sum of digits.
function sum($n){
$sum = 0;
while($n > 0){
$sum += $n % 10;
$n = $n / 10;
}
echo '<br>'.$sum.'<br>';
}
$n = 123;
sum($n);
// c) print factorial of a number.
function fact($n){
$fact = 1;
for($i = 1; $i <= $n; $i++){
$fact *= $i;
}
echo '<br>'.$fact.'<br>';
}
$n = 5;
fact($n);
// d) display text messages.
echo "Hello World<br>";
// e) print an array.
$arr = array(1, 2, 3, 4, 5);
foreach($arr as $a){
echo $a;
}
echo '<br>';
// f) create a simple calculator
function calc($a, $b, $op){
switch($op){
case '+':
echo $a + $b;
break;
case '-':
echo $a - $b;
break;
case '*':
echo $a * $b;
break;
case '/':
echo $a / $b;
break;
default:
echo "Invalid Operator";
}
}
$a = 5;
$b = 2;
calc($a, $b, '+');
echo '<br>';
// g) find vowel checking a string.
function vowel($str){
$vowels = array('a', 'e', 'i', 'o', 'u');
$count = 0;
for($i = 0; $i < strlen($str); $i++){
if(in_array($str[$i], $vowels)){
$count++;
}
}
echo $count;
}
$str = "Hello World";
vowel($str);
?>