-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_for_loop.php
45 lines (45 loc) · 1.09 KB
/
example_for_loop.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
<?php
/*
question 1
Write a PHP program to print the odd numbers from 1 to 99. Prints one number per line.
*/
for ( $i = 1; $i < 100; $i=$i+2 ) {
echo $i."<br>";
}
///////////////////////////////////////////////////////////////////
/*
question 2
Write a PHP program to print numbers between 1 to 100 which are divisible by 3, 5 and by both
*/
$divisible_by_3=[];
$divisible_by_5=[];
$divisible_by_both=[];
for ($i=1;$i<=100;$i++) {
if($i%3==0){
$divisible_by_3[]=$i;
}
if($i%5==0){
$divisible_by_5[]=$i;
}
if($i%3==0&&$i%5==0){
$divisible_by_both[]=$i;
}
}
//to print array contain all numbers divisible by 3 from 1 to 100
echo "Divided by 3: <br>";
foreach ($divisible_by_3 as $item){
echo $item." ";
}
echo "<hr>";
//to print array contain all numbers divisible by 5 from 1 to 100
echo "Divided by 5: <br>";
foreach ($divisible_by_5 as $item){
echo $item." ";
}
echo "<hr>";
//to print array contain all numbers divisible by 3 and 5 from 1 to 100
echo "Divided by 3 and 5: <br>";
foreach ($divisible_by_both as $item){
echo $item." ";
}
?>