PWD || PHP : Membuat deret bilangan gajil dan genap.

Mujahid Habibullah
Sep 5, 2018 · 1 min read

Contoh hasil Output jika kita masukkan nilai deret ganjil sebanyak 5 kali adalah sebagai berikut:

Untuk membuat deret seperti contoh diatas kita membutuhkan kodingan seperti berikut:

<form action=” “ method=”GET”>
Pilih Deret (Ganjil/Genap):<input type=”text” name=”deret”><br>//meminta jenis deret
Banyak Bilangan : <input type=”text” name=”banyak”>//banyaknya jumlah deret
<input type=”Submit” name=”submit” value=”Show”>
</form>

<?php
if (isset($_GET[‘submit’])){//mengambil nilai dari submit
$x=$_GET[‘deret’];//mengambil nilai banyaknya deret

function gan($n,$i=0,$j=0){
if($i<$n){
if($i==0){
echo $j+=1;
}else
echo $j+=2;
$i++;
gan($n,$i,$j);
}
}
function gen($n,$i=0,$j=0){
if($i<$n){
if($i==0){
echo $j+=2;
}else
echo $j+=2;
$i++;
gan($n,$i,$j);
}
}

switch ($x) {
case ‘Ganjil’:
echo “Deret Ganjil “;
gan($_GET[‘banyak’]);
break;
case ‘Genap’:
echo “Deret Genap “;
gen($_GET[‘banyak’]);
break;
default:
break;
}
}
?>

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade