Setelah kemarin sudah memposting Program Konversi Desimal ke Biner, sekarang program Biner ke Desimal menggunakan Java.
package tugasalpro1;
import java.util.Scanner;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author moko
*/
public class binertodesimal {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//inisialisai
int biner, i = 0, cek = 0;
long nilai = 0;
biner = input.nextInt();
//mencari 2 pangkat n
do{
if(biner - (i * Math.pow(10, i)) >= 0){
cek = cek+1;
}
i++;
}while(i <= biner);
for (int j = cek; j >= 0; j--) {
// n = biner - 10 pangkat j
int n = (int) (biner - Math.pow(10,j));
// k = 10 pangkat j
int k = (int) Math.pow(10, j);
if(n >= 0){
nilai = (int) (nilai + Math.pow(2, j));
biner = biner - k;
}
}
System.out.println(nilai);
}
}
0 komentar:
Posting Komentar