Perl script to check if a number is a power of 2

my $string = 1;

my $sum =1;

while ($sum < $string)
{
$sum=$sum * 2;
}
if($sum eq $string)
{
print “The number is 2nd power”;
}
else{
print “The number is not 2nd power\n”;
print “The nearest power 2 is $sum”;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s