Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
28 views

Details Binarytodecimal: Public Class Public Int Int Int Decimal Int While True If Break Else Int Decimal

This Java code defines a Details class with a BinaryToDecimal method that converts a binary number to decimal. The method uses a while loop to iterate through each digit of the binary number, multiplying it by the corresponding power of 2 and adding it to the decimal total. The main method creates a Details object and calls BinaryToDecimal to convert several example binary numbers and print the results.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Details Binarytodecimal: Public Class Public Int Int Int Decimal Int While True If Break Else Int Decimal

This Java code defines a Details class with a BinaryToDecimal method that converts a binary number to decimal. The method uses a while loop to iterate through each digit of the binary number, multiplying it by the corresponding power of 2 and adding it to the decimal total. The main method creates a Details object and calls BinaryToDecimal to convert several example binary numbers and print the results.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

public class Details {

public int BinaryToDecimal(int binaryNumber){


int decimal = 0;
int p = 0;
while(true){
if(binaryNumber == 0){
break;
} else {
int temp = binaryNumber%10;
decimal += temp*Math.pow(2, p);
binaryNumber = binaryNumber/10;
p++;
}
}
return decimal;
}
public static void main(String args[]){
Details obj = new Details();
System.out.println("110 --> "+obj.BinaryToDecimal(110));
System.out.println("1101 --> "+obj.BinaryToDecimal(1101));
System.out.println("100 --> "+obj.BinaryToDecimal(100));
System.out.println("110111 --> "+obj.BinaryToDecimal(110111));
}

You might also like