Rabu, 01 Juni 2011

[share ilmu] Desimal to Biner with java



Source Code :

import java.io.*;
class DesBin
{
            private int angka;
            public DesBin()
            {
                        this.angka=0;
            }
            private String getBiner(int angka)
            {
                        String biner = "";
                        while(angka != 0)
                        {
                                    if(angka % 2 != 0)
                                    {
                                                biner +=  "1";
                                    }
                                    else
                                                biner += "0";
                                                angka /= 2;
                        }
            return new StringBuffer(biner).reverse().toString();
            }
            public static void main(String[] args) throws IOException
            {
                        DesBin DTB = new DesBin();
                        DataInputStream input = new DataInputStream(System.in);
                        System.out.print ("Masukkan bilangan Desimal : ");
                        String des = input.readLine();
                        int angka = Integer.parseInt(des);
                        System.out.println("Angka Biner : "+DTB.getBiner(angka));
            }
}

0 comments:

Posting Komentar

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites