Question
Program to convert decimal to binary
ENTER DECIMAL NUMBER:1234
THE BINARY VALUE OF DECIMAL 1234 IS 10011010010
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
Java
Python
Java
import java.util.Scanner;
public class DecimalToBinary
{
public static void main()
{
int count=0;
Scanner sc=new Scanner(System.in);
long binary=0,decimal=0,temp=0;
System.out.print("ENTER DECIMAL NUMBER:");
decimal=sc.nextInt();
temp=decimal;
count=0;
while(temp>0)
{
binary=binary+(temp%2)*((long)Math.pow(10,count));
count++;
temp=temp/2;
}
System.out.println("THE BINARY VALUE OF DECIMAL "+decimal+" IS "+binary);
}
}
Python
decimalNumber=int(input("Enter Decimal number:"))
count=0
binaryNumber=0
temp=decimalNumber
while(temp>0):
rem=temp%2
binaryNumber=binaryNumber+rem*(10**count)
count=count+1
temp=temp//2
print("Binary value of Decimal number:",decimalNumber,"is:",binaryNumber)