/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package binary_to_decaimal;
import java.util.*;
public class Binary_to_Decaimal {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int n,dec=0,i=0,N=0;
Scanner in=new Scanner(System.in);
System.out.println("Enter number in binary form like 0 1 or 1 0");
n=in.nextInt();
while(n > 0)
{
dec = n % 10;
N =N+ dec *( int)Math.pow(2,i);
i++;
n=n/10;
}
System.out.println("Decimal \t"+N);
}
}
当二进制数为11(3)并得到所需的十进制数时,如何在程序上运行?
答案 0 :(得分:0)
可能会对您有所帮助:
public int getDecimalFromBinary(int binary){
int decimal = 0;
int power = 0;
while(true){
if(binary == 0){
break;
} else {
int tmp = binary%10;
decimal += tmp*Math.pow(2, power);
binary = binary/10;
power++;
}
}
return decimal;
}