Assalamu'alaikum selamat malam semua..
Pada kesempatan kali ini saya akan memposting tugas ke-empat saya dari mata kuliah Web Programming, yaitu :
- Membuat segitiga angka menggunakan perintah for dan while pada PHP dan,
- Menghitung nilai faktorial menggunakan perintah for, while, dan do while pada PHP.
Berikut saya lampirkan script nya.
1. Membuat segitiga angka menggunakan perintah for pada PHP
<html>
<body>
<title> For </title>
<h1> Segitiga For </h1>
<?php
$x = 1;
$y = 7;
$z = 1;
for($x=$x;$x<$y;$x++)
{
for($z=1;$z<=$x;$z++)
{
$tampil = $x;
echo $tampil;
}
// Tambahkan enter
echo "<BR>";
}
?>
</body>
</html>
Tampilannya:
2. Membuat segitiga angka menggunakan perintah while pada PHP
<html>
<body>
<title> While </title>
<h1> Segitiga while </h1>
<?php
$a=1;
while($a<=1)
{ echo "<font size=</h3>1</font><h3>";
$a++;}
{ echo "<font size=</h3>22</font><h3>";
$a++;}
{ echo "<font size=</h3>333</font><h3>";
$a++;}
{ echo "<font size=</h3>4444</font><h3>";
$a++;}
{ echo "<font size=</h3>55555</font><h3>";
$a++;}
?>
</body>
</html>
Tampilannya:
3. Menghitung nilai faktorial menggunakan perintah for pada PHP
<!DOCTYPE html>
<html>
<head>
<title>Faktorial</title>
</head>
<body>
<form name="frm01" method="post">
<table>
<tr>
<td>Masukkan Angka</td>
<td><input type="text" name="txtAngka" size="5"/></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="hasil" value="Hasil"/></td>
</tr>
</table>
<?php
error_reporting(0);
if($_POST['hasil']!=''){
$angka=$_POST['txtAngka'];
function faktorial($angka){
if($angka<=1){
$hasil=1;
return $hasil;
}elseif($angka>1){
for($i=1; $i<=$angka; $i++){
$hasil=$angka * faktorial($angka-1);
}
return $hasil;
}
}
echo "Angka :".$angka."<br>";
echo "Faktorial dari "." ".$angka." "."adalah :"." ".faktorial($angka);
}
?>
</form>
</body>
</html>
Tampilannya:
4. Menghitung nilai faktorial menggunakan perintah while pada PHP
<html>
<head>
<title>Menghitung Faktorial</title>
<style>
body{
color:#000;
background-image:url(bunga.jpg);
background-attachment:fixed;
font-size:18px;
font-family: Tahoma, sans-serif;
margin:0;
padding:48px;
}
h2{
color:white;
}
h2:hover{
color:black;
}
a{
text-decoration:none;
}
a:hover{
color:black;
}
.panel{
max-width:480px;
background-color:#0b1;
margin:1px auto 1px;
padding:24px;
border-radius:12px;
-webkit-border-radius:8px;
-moz-border-radius:8px;
box-shadow:0 4px 8px black;
}
input{
border-radius:8px;
-webkit-border-radius:8px;
-moz-border-radius:8px;
font-size:18px;
padding:8px 14px;
margin:0 1px;
border:1px solid #888;
}
.field{
width:320px;
}
.tombol{
color:#fff;
background-color:#000;
}
.tombol:hover{
background-color:brown;
}
</style>
</head>
<body>
<?php
$n = isset($_POST['n']) ? $_POST['n'] : NULL;
if(isset($_POST['submit'])){
if($n!=NULL){
$bil = 1;
$i=0;
while($i<$n){
$i++;
$bil = $bil*$i;
}
}else{
$bil = 'Harap masukkan bilangan!!';
}}
date_default_timezone_set('Asia/Jakarta');
echo '<div class="panel">';
echo '<center><h2>Menghitung Bilangan Faktorial menggunakan While pada PHP</h2></center>';
echo date("l, d-M-Y H:i:s");
echo '<hr>';
echo '<h4>Masukkan Bilangan :</h4>';
echo '<form action="" method="post">';
echo '<input class="field" type="text" name="n" value="'.$n.'" placeholder="Masukkan Bilangan..."/>';
echo '<input class="tombol" type="submit" name="submit" value="Hitung"/>';
echo '</form>';
if((isset($n))and($n!=NULL)){
echo '<h4>Hasil Faktorial :</h4>';
echo '=> ';
$i=0;
while($i<$n-1){
$i++;
echo $i.'x';
}
echo $n;
echo '<br/>';
}
echo '<h4>Jumlah Faktorial :</h4>';
echo '<input class="field" type="text" value="'.(isset($bil) ? $bil : NULL).'" readonly/>';
echo '<hr>';
echo '<center><a href="http://ulilazmii.blogspot.com">© UlilAzmi14090064</a></center>';
echo '</div>';
?>
</body>
</html>
Tampilannya:
5. Menghitung nilai faktorial menggunakan perintah do while pada PHP
<html>
<head>
<title>Menghitung Bilangan Faktorial menggunakan Do While pada PHP</title>
<style>
body{
color:#000;
background-image:url(bunga.jpg);
background-attachment:fixed;
font-size:18px;
font-family: Tahoma, sans-serif;
margin:0;
padding:48px;
}
h2{
color:white;
}
h2:hover{
color:black;
}
a{
text-decoration:none;
}
a:hover{
color:black;
}
.panel{
max-width:480px;
background-color:#0b1;
margin:1px auto 1px;
padding:24px;
border-radius:12px;
-webkit-border-radius:8px;
-moz-border-radius:8px;
box-shadow:0 4px 8px black;
}
input{
border-radius:8px;
-webkit-border-radius:8px;
-moz-border-radius:8px;
font-size:18px;
padding:8px 14px;
margin:0 1px;
border:1px solid #888;
}
.field{
width:320px;
}
.tombol{
color:#fff;
background-color:#000;
}
.tombol:hover{
background-color:brown;
}
</style>
</head>
<body>
<?php
$n = isset($_POST['n']) ? $_POST['n'] : NULL;
if(isset($_POST['submit'])){
if($n!=NULL){
$bil = 1;
$i=1;
do
{
$bil = $bil*$i;
$i++;
}
while($i<=$n);
}else{
$bil = 'Harap masukkan bilangan!!';
}
}
date_default_timezone_set('Asia/Jakarta');
echo '<div class="panel">';
echo '<center><h2>Menghitung Bilangan Faktorial menggunakan Do While</h2></center>';
echo date("l, d-M-Y H:i:s");
echo '<hr>';
echo '<h4>Masukkan Bilangan :</h4>';
echo '<form action="" method="post">';
echo '<input class="field" type="text" name="n" value="'.$n.'" placeholder="Masukkan Bilangan..."/>';
echo '<input class="tombol" type="submit" name="submit" value="Hitung"/>';
echo '</form>';
if((isset($n))and($n!=NULL)){
echo '<h4>Hasil Faktorial :</h4>';
echo '=> ';
$i=1;
do{
echo $i.'x';
$i++;
}while($i<$n);
echo $n;
echo '<br/>';
}
echo '<h4>Jumlah Faktorial :</h4>';
echo '<input class="field" type="text" value="'.(isset($bil) ? $bil : NULL).'" readonly/>';
echo '<hr>';
echo '<center><a href="http://ulilazmii.blogspot.com">© UlilAzmi14090064</a></center>';
echo '</div>';
?>
</body>
</html>
Tampilannya:
Nah, kurang lebih seperti itu tugas saya. Mungkin terlihat sangat sederhana, karna baru segitu kemampuan saya. Harap dimaklumin hehe :D
Oke sekian postingan saya hari ini, tunggu postingan saya selanjutnya di lain waktu. Wassalamu'alaikum ;))