Sabtu, 11 Agustus 2012

Konversi Bilangan Desimal ke Biner

By Intan Della di 10:09:00 AM
Sekarang giliran Desimal ke Biner.


1. Buka aplikasi Neatbeans.
2. Bikin project baru dengan cara klik kanan pada folder >> New >> PHP Web Page.




3. Beri nama file Konversi_Des_to_Biner.
4. 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
}
?>

5. Seperti ini tampilan di Netbeans.


6. Tekan Shift+F6, untuk menjalankan aplikasi. Maka akan tampil di browser seperti ini.



 7. Masukkan angka seperti di bawah ini.


 8. Maka akan muncul hasil seperti dibawah ini.


Oke. Selamat Mencoba :)

0 komentar:

Posting Komentar

 

d' Vain Copyright © 2013 Design by Inde