3. Write a program that asks for a decimal number less than 256 and converts it to binary in perl. (Hint: You may want to use the bitwise and operator 8 times.).
print "Enter a decimal number \n";
my $dec_number = <STDIN>;
my $i = 0 ;
my $result = 0 ;
chomp($dec_number);
print "The decimal number:",$dec_number,"\n";
if( $dec_number < 256 )
{
for( $i = 7; $i >= 0; $i = $i - 1 )
{
$result = $dec_number >> $i; #right shift
if ($result & 1){
print "1";
}
else{
print "0";
}
}
print "\n";
}
else{
print "Please enter numbers less than or equal to 256";
}
Any query you can ask in the comment.
Get Answers For Free
Most questions answered within 1 hours.