So you've got a binary number on your hands, huh? Well, don't worry - converting it to a decimal number is easier than you might think! Just remember that binary is a way of counting using only 2 digits (0 and 1), whereas decimal uses 10 digits (0 through 9). To convert from binary to decimal, you'll need to convert each "place value" of the binary number into its corresponding decimal value. For example, if you have the binary number 11011, you would start by dividing it up into its place values: the rightmost digit represents the "ones" place, the next digit over represents the "twos" place, then "fours", then "eights", and so on. Then just multiply each digit by its corresponding power of 2 and add them all up! So for our example number, we'd get: 1x1 + 1x2 + 0x4 + 1x8 + 1x16 = 27. And there you have it - an easy and breezy conversion from binary to decimal!
#include <stdio.h>
int main() {
long long binaryNumber, decimalNumber = 0, base = 1, remainder;
printf("Enter a binary number: ");
scanf("%lld", &binaryNumber);
while (binaryNumber > 0) {
remainder = binaryNumber % 10;
decimalNumber += remainder * base;
binaryNumber /= 10;
base *= 2;
}
printf("Decimal equivalent: %lld", decimalNumber);
return 0;
}
#include <iostream>
using namespace std;
int main() {
long long binaryNumber, decimalNumber = 0, base = 1, remainder;
cout << "Enter a binary number: ";
cin >> binaryNumber;
while (binaryNumber > 0) {
remainder = binaryNumber % 10;
decimalNumber += remainder * base;
binaryNumber /= 10;
base *= 2;
}
cout << "Decimal equivalent: " << decimalNumber;
return 0;
}
def main():
binary_number = input("Enter a binary number: ")
decimal_number = int(binary_number, 2)
print("Decimal equivalent:", decimal_number)
if __name__ == "__main__":
main()
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a binary number: ");
String binaryString = scanner.next();
int decimalNumber = Integer.parseInt(binaryString, 2);
System.out.println("Decimal equivalent: " + decimalNumber);
}
}