Interviews Questions, Algorithms, Aptitude, C Interview Program, C Theory Question, Aptitude Tricks, Test Series,

Sunday, 28 April 2019

IOPP Program No.8.B

Program No.8.B 

Write a Perl script to convert the decimal number to binary number

Solution:

print"Enter a number to convert : ";
$num=<STDIN>;

$intnum=sprintf("%d",$num);

$i=0;

while($intnum>0)
{
        $array[$i]=$intnum%2;
        $intnum=int($intnum/2);
        $i=$i+1;
}

print "Binary value : ";
for($j=$i;$j>=0;$j--)
{
        print "$array[$j]";

}

0 comments:

Post a Comment