This program prints diamond pattern of alphabets, validations using PHP. This program checks the input value is even number between 2 and 24 then prints the diamond pattern of alphabets.
<html>
<head>
<style type="text/css">
.but {
border-radius: 20px;
width: 250px;
height: 40px;
font-weight: bolder;
font-size: 20px;
}
</style>
<body>
<form id="formd" method="get" action="<?php $_PHP_SELF ?>">
<label>Diamond No.:</label>
<input type="text" name="dia" maxlength="2" placeholder = "0" />
<button type="submit" class="but" form="formd" value="submit">Make Diamonds</button>
</form>
</body>
</html>
<?php
$val = $_GET["dia"];
if (($val<25) && ($val>1)) {
$t = $val%2;
if ($t==0) {
drawDiamond($val);
}
else {
echo "Only EVEN numbers between 2 and 24 are allowed";
}
}
else {
echo "Only EVEN numbers between 2 and 24 are allowed";
}
function drawDiamond($a) {
$str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$count= 0;
echo "<pre>";
for ($i= 1;$i<=($a/2);$i++) {
for ($s=($a/2); $s>=$i; $s--) {
print(" ");
}
for ($j= 1; $j<(2*$i); $j++) {
print("$str[$count]");
}
$count++;
print ("\n");
}
for ($p=1;$p<=$a+1;$p++) {
print("$str[$count]");
}
print("\n");
$h=1;
for ($d=1;$d<=($a/2);$d++) {
for ($e=1;$e<=$d;$e++) {
print(" ");
}
$count--;
for ($f=($a-$h);$f>=1;$f--) {
print("$str[$count]");
}
$h=$h+2;
print("\n");
}
echo "</pre>";
}
?>
<html>
<head>
<style type="text/css">
.but {
border-radius: 20px;
width: 250px;
height: 40px;
font-weight: bolder;
font-size: 20px;
}
</style>
<body>
<form id="formd" method="get" action="<?php $_PHP_SELF ?>">
<label>Diamond No.:</label>
<input type="text" name="dia" maxlength="2" placeholder = "0" />
<button type="submit" class="but" form="formd" value="submit">Make Diamonds</button>
</form>
</body>
</html>
<?php
$val = $_GET["dia"];
if (($val<25) && ($val>1)) {
$t = $val%2;
if ($t==0) {
drawDiamond($val);
}
else {
echo "Only EVEN numbers between 2 and 24 are allowed";
}
}
else {
echo "Only EVEN numbers between 2 and 24 are allowed";
}
function drawDiamond($a) {
$str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$count= 0;
echo "<pre>";
for ($i= 1;$i<=($a/2);$i++) {
for ($s=($a/2); $s>=$i; $s--) {
print(" ");
}
for ($j= 1; $j<(2*$i); $j++) {
print("$str[$count]");
}
$count++;
print ("\n");
}
for ($p=1;$p<=$a+1;$p++) {
print("$str[$count]");
}
print("\n");
$h=1;
for ($d=1;$d<=($a/2);$d++) {
for ($e=1;$e<=$d;$e++) {
print(" ");
}
$count--;
for ($f=($a-$h);$f>=1;$f--) {
print("$str[$count]");
}
$h=$h+2;
print("\n");
}
echo "</pre>";
}
?>
No comments:
Post a Comment