Pages

Saturday, August 11, 2012

Konversi Bilangan Decimal to Biner

Kemarin kan udah posting tentang Konversi Bilangan Decimal to Hexa sama Konversi Bilangan Decimal to Oktal sekarang waktunya Konversi Bilangan Decimal to Biner. Langsung aja yukk ...

1. Buka Aplikasi Netbeans kalian.
2. Buat Project baru dengan cara klik kanan pada folder > New > PHP Web Page.


3. Beri nama file dengan Konversi_Des_to_Biner.
4. Setelah itu klik Finish.
5. Lalu masukkan script di bawah ini.


<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<?php
if (isset($_POST['decimal'])) {
    $decimal =$_POST['decimal'];
    $original =$_POST['decimal'];
    $binary='';
    if (preg_match('/[^0-9]/', $decimal)){
        die("Maaf. Inputan salah..");       
    }
    else {
        while ($decimal > 0){
            if ($decimal%2 == 0){
                $binary .= 0;
                $decimal /= 2;                
            }
            else {
                $binary .= 1;
                $decimal = ($decimal/2)-0.5;                
            }
        }
        $result = strrev($binary);
        echo "Bilangan $original (desimal) dalam biner adalah $result.
        <a href='Konversi_Des_to_Biner.php'>Back</a> to the script";
    }
}
else {    
?>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <form action="<?php echo $_SERVER['PHP_SELF']; ?>"
              method="POST">
            <h1> masukkan bilangan Desimal sisini (cepat!!!):
            </h1><input type="text" size="50" name="decimal">
            <input type="submit" value="Konversikan!">
        </form>
        <?php
        echo $_SERVER['PHP_SELF'];
        ?>
    </body>
</html>
<?php
}
?>

6. Maka tampilan di Netbeans seperti ini.


7. Kalau sudah, tekan Shift+F6, maka akan muncul di browser seperti di bawah ini.


8. Masukkan angka seperti pada gambar.


9. Maka akan tampil hasil seperti dibawah ini.


Selamat Mencoba... Bertemu di pelajaran selanjutnya :)
Comments
0 Comments

No comments:

Post a Comment

berkomentarlah dengan baik, maka saya akan berterima kasih :)