convert the following number into their equivalent decimal number. (i) -(10101)2
Answers
Answer:
An easy method of converting decimal to binary number equivalents is to write down the decimal number and to continually divide-by-2 (two) to give a result and a remainder of either a “1” or a “0” until the final result equals zero. So for example. Convert the decimal number 29410 into its binary number equivalent.
21 10101 25
Answer:
Code to convert binary to decimal in python, C++ and java is
Explanation:
1. Python :
def binaryToDecimal(n):
num = n;
dec_value = 0;
base = 1;
temp = num;
while(temp):
last_digit = temp % 10;
temp = int(temp / 10);
dec_value += last_digit * base;
base = base * 2;
return dec_value;
num = 10101;
print("Binary to decimal conversion for 10101 is : " ,binaryToDecimal(num) );
2. C++
#include <iostream>
using namespace std;
int binaryToDecimal(int n)
{
int num = n;
int dec_value = 0;
int base = 1;
int temp = num;
while (temp) {
int last_digit = temp % 10;
temp = temp / 10;
dec_value += last_digit * base;
base = base * 2;
}
return dec_value;
}
int main()
{
int num = 10101;
cout << binaryToDecimal(num) << endl;
}
3. Java
class GFG {
static int binaryToDecimal(int n)
{
int num = n;
int dec_value = 0;
int base = 1;
int temp = num;
while (temp > 0) {
int last_digit = temp % 10;
temp = temp / 10;
dec_value += last_digit * base;
base = base * 2;
}
return dec_value;
}
public static void main(String[] args)
{
int num = 10101;
System.out.println(binaryToDecimal(num));
}
}
#SPJ2