Question
Convert Binary to a decimal using recursion and vice versa.
Press 1 to convert binary to decimal
Press 2 to convert decimal to binary
1
ENTER BINARY NUMBER:1111101
THE DECIMAL VALUE OF BINARY 1111101 IS 125
Press 1 to convert Binary to Decimal
Press 2 to convert Decimal to Binary
Enter your choice:2
Enter the Decimal number:125
The binary value of 125 is 1111101
Press 1 to convert Binary to Decimal
Press 2 to convert Decimal to Binary
Enter your choice:2
Enter the Decimal number:-125
The binary value of -125 is -1111101
Press 1 to convert Binary to Decimal
Press 2 to convert Decimal to Binary
Enter your choice:1
Enter the Binary number:-1111101
The decimal value of -1111101 is -125
Press 1 to convert Binary to Decimal
Press 2 to convert Decimal to Binary
Enter your choice:1
Enter the Binary number:120101
Invalid Binary number
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
Java
Python
Java
import java.util.*;
public class DecimalToBinaryAndViceVersaUsingRecursion
{
public static boolean isBinaryNumbervalid(long bin)
{
while(bin>0)
{
if(bin%10>1)
{
System.out.println("Invalid binary number");
return false;
}
bin=bin/10;
}
return true;
}
public static long BinaryToDecimalConversion(long bin,int power)
{
if(bin==0)
{
return 0;
}
else
{
return ((bin%10)*(long)Math.pow(2,power))+BinaryToDecimalConversion(bin/10,power+1);
}
}
public static long DecimalBinaryConversion(long dec)
{
if(dec==0)
{
return 0;
}
else
{
return (dec%2)+10*(DecimalBinaryConversion(dec/2));
}
}
public static void main()
{
int choice=0;
long decimal=0,binary=0;
Scanner sc=new Scanner(System.in);
System.out.println("Press 1 to convert binary to decimal");
System.out.println("Press 2 to convert decimal to binary");
System.out.println("Enter your choice:");
choice=sc.nextInt();
switch(choice)
{
case 1:
System.out.print("ENTER BINARY NUMBER:");
binary=sc.nextLong();
if(binary< 0)//negative binary number
{
if(isBinaryNumbervalid(-binary)==true)
{
decimal=-BinaryToDecimalConversion(-binary,0);
/*
BinaryToDecimalConversion(-binary,0) will return positive decimal number
but we want negative decimal number so that's why
we added negative sign in front of BinaryToDecimalConversion(-binary,0)
*/
System.out.println("THE DECIMAL VALUE OF BINARY "+binary+" IS "+decimal);
}
}
else
{
if(isBinaryNumbervalid(binary)==true)
{
decimal=BinaryToDecimalConversion(binary,0);
System.out.println("THE DECIMAL VALUE OF BINARY "+binary+" IS "+decimal);
}
}
break;
case 2:
System.out.print("ENTER DECIMAL NUMBER:");
decimal=sc.nextLong();
if(decimal< 0)
{
binary=-DecimalBinaryConversion(-decimal);
/*
DecimalBinaryConversion(-decimal) will return positive binary number
but we want negative binary number so that's why
we added negative sign in front of DecimalBinaryConversion(-decimal)
*/
System.out.println("THE BINARY VALUE OF DECIMAL NUMBER "+decimal+" IS "+binary);
}
else
{
binary=DecimalBinaryConversion(decimal);
System.out.println("THE BINARY VALUE OF DECIMAL NUMBER "+decimal+" IS "+binary);
}
break;
default:
System.out.println("Invalid Choice");
break;
}
}
}
Python
def isBinaryNumberValid(binary):
if(binary==0):
return True
if(binary%10>1):
print("Invalid Binary number")
return False
else:
return isBinaryNumberValid(binary//10)
def binaryToDecimalConvertor(binary,power=0):
if(binary==0):
return 0
else:
remainder=binary%10
return remainder*(2**power)+binaryToDecimalConvertor(binary//10,power+1)
def decimalToBinaryConvertor(decimal):
if(decimal==0):
return 0
else:
remainder=decimal%2
return remainder+10*decimalToBinaryConvertor(decimal//2)
if(__name__=='__main__'):
print("Press 1 to convert Binary to Decimal")
print("Press 2 to convert Decimal to Binary")
choice=int(input("Enter your choice:"))
if(choice==1):
binaryNumber = int(input("Enter the Binary number:"))
if (binaryNumber < 0):
if (isBinaryNumberValid(-binaryNumber) == True):
decimalNumber = -binaryToDecimalConvertor(-binaryNumber)
# binaryToDecimalConvertor(-binaryNumber) will return positive decimal number
# but we want negative decimal number so that's why
# we added negative sign in front of binaryToDecimalConvertor(-binaryNumber)
print("The decimal value of", binaryNumber, "is", decimalNumber)
else:
if (isBinaryNumberValid(binaryNumber) == True):
decimalNumber = binaryToDecimalConvertor(binaryNumber)
print("The decimal value of", binaryNumber, "is", decimalNumber)
elif(choice==2):
decimalNumber = int(input("Enter the Decimal number:"))
if (decimalNumber < 0):
binaryNumber = -decimalToBinaryConvertor(-decimalNumber)
# decimalToBinaryConvertor(-decimalNumber) will return positive binary number
# but we want negative binary number so that's why
# we added negative sign in front of decimalToBinaryConvertor(-decimalNumber)
print("The binary value of", decimalNumber, "is", binaryNumber)
else:
binaryNumber = decimalToBinaryConvertor(decimalNumber)
print("The binary value of", decimalNumber, "is", binaryNumber)
else:
print("Invalid Choice Entered")
Coding Store
Sale

ISC QUESTION PAPERS WITH SOLUTION(PROGRAMMING ONLY)
Sale

ICSE QUESTION PAPER WITH SOLUTION(PROGRAMMING ONLY)
Sale

ISC QUESTION PAPERS WITH SOLUTION(PROGRAMMING ONLY)
Sale

ICSE QUESTION PAPER WITH SOLUTION(PROGRAMMING ONLY)
Sale

ISC QUESTION PAPERS WITH SOLUTION(PROGRAMMING ONLY)
Sale
