. .

TUGAS DAN LATIHAN:  BAHASA PEMROGRAMAN JAVA Netbeans IDE MENGKONVERSI BILANGAN DECIMAL KE BILANGAN BINARY  

Posted by CHRISTIANTO D,WIBOWO | D3MI-2016 STMIK BUMI GORA On Minggu, Desember 04, 2016 No comments

Tugas lagi !!!
Buatlah dengan 4 cara untuk mengkonversi bilangan desimal ke bilangan biner :
  1. Mengkonversi dengan menulis logika sendiri tanpa menggunakan metode yang telah ditetapkan - without predefined method.
  2. Menggunakan toBinaryString() method dari Integer class.
  3. Menggunakan Stack
  4. Menggunakan Metode Input (import java.util.Scanner;)
Source Code Method1: Without using predefined method
package decimalbinaryexample;
// @author Christianto Dharma Wibowo - GemblongS
public class DecimalBinaryExample {
public void convertBinary(int num){
     int binary[] = new int[40];
     int index = 0;
     while(num > 0){
       binary[index++] = num%2;
       num = num/2;
     }
     for(int i = index-1;i >= 0;i--){
       System.out.print(binary[i]);
     }
  }

  public static void main(String a[]){
     DecimalBinaryExample obj = new DecimalBinaryExample();
     System.out.println("Binary representation of 124: ");
     obj.convertBinary(124);
     System.out.println("\nBinary representation of 45: ");
     obj.convertBinary(45);
     System.out.println("\nBinary representation of 999: ");
     obj.convertBinary(999);
  }
}
Output :

Source Code Method2: Menggunakan toBinaryString() method
package decimalbinaryexample;
// @author Christianto Dharma Wibowo - GemblongS
public class DecimalBinaryExamples {
public static void main(String a[]){
    System.out.println("Binary representation of 124: ");
    System.out.println(Integer.toBinaryString(124));
        System.out.println("\nBinary representation of 45: ");
        System.out.println(Integer.toBinaryString(45));
        System.out.println("\nBinary representation of 999: ");
        System.out.println(Integer.toBinaryString(999));
    }
}
Output :

Source Code Method3: Menggunakan Stack
package decimalbinaryexample;
import java.util.*;
// @author Christianto Dharma Wibowo - GemblongS
public class DecimalBinaryStack {
public static void main(String[] args)
  {
    Scanner in = new Scanner(System.in);

    // Create Stack object
    Stack<Integer> stack = new Stack<Integer>();

    // User input
    System.out.println("Enter decimal number: ");
    int num = in.nextInt();

    while (num != 0)
    {
      int d = num % 2;
      stack.push(d);
      num /= 2;
    }

    System.out.print("\nBinary representation is:");
    while (!(stack.isEmpty() ))
    {
      System.out.print(stack.pop());
    }
    System.out.println();
  }
}
Output :

Source Code Method3: Menggunakan Metode Input (import java.util.Scanner;)
package decimaltobinary;
/**
 * @author Christianto Dharma Wibowo - GemblongS
 */
import java.util.Scanner;
public class DecimalToBinary {
public String toBinary(int n) {
       if (n == 0) {
           return "0";
       }
       String binary = "";
       while (n > 0) {
           int rem = n % 2;
           binary = rem + binary;
           n = n / 2;
       }
       return binary;
   }

   public static void main(String[] args) {
       Scanner scanner = new Scanner(System.in);
       System.out.print("Enter a number: ");
       int decimal = scanner.nextInt();
       DecimalToBinary decimalToBinary = new DecimalToBinary();
       String binary = decimalToBinary.toBinary(decimal);
       System.out.println("The binary representation is " + binary);

   }
}
Output :

Selesai sudah, semoga berguna bagi kita semua. - TAMPIASIH -

0 komentar:

Posting Komentar