Jumat, 22 November 2013

KONVERSI BILANGAN DESIMAL KE BINER,



KONVERSI BILANGAN  DESIMAL KE BINER ( SOAL UTS NO 5 ) /STRUKTUR DATA.
 package javaapplication11;
    import java.util.NoSuchElementException;
    import java.util.Scanner;
    /**
    *
    * @author EKO KURNIAWAN
    */
    public class JavaApplication11 {
    private class Element {
    public Object data;
    public Element next;
    public Element( Object data, Element next ) {
    this.data = data;
    this.next = next;
      }
    }
    private Element top;
    public void push( Object data ) {
    if( isEmpty( ) ) {
    top = new Element( data, null );
    } else {
    top = new Element( data, top );
      }
    }
 public Object pop( ) throws NoSuchElementException {
    if( isEmpty( ) ) {
    throw new NoSuchElementException( );
    } else {
    Object data = top.data;
    top = top.next;
    return data;
      }
    }
 public Object peek( ) throws NoSuchElementException {
    if( isEmpty( ) ) {
    throw new NoSuchElementException( );
    } else {
    return top.data;
      }
    }
 public boolean isEmpty( ) {
    return top == null;
    }
 public static void main(String args[]) {
    System.out.println("Program Konversi Bilangan Desimal ke Biner");
    System.out.println("---------------------------------------------------");
    Scanner input = new Scanner(System.in);
    System.out.print("Masukkan Angka -> ");
    int bil = input.nextInt();
    int c = bil;
JavaApplication11 stack = new JavaApplication11();
    if (bil == 0) {
    System.out.println("0");
    }
    while (bil != 0) {
    int a = bil / 2;
    int b = bil % 2;
    stack.push(b);
    bil = a;
    }
System.out.print("Biner dari Angka "+c+" = ");
    while (!stack.isEmpty()) {
    System.out.print(stack.pop());
    }
 System.out.println(" ");
      }
    }

0 komentar:

Posting Komentar