you are given a number. it's a simple integer, like 6 or 144 (positive). you're told to round it up to the nearest power of 2. for 6 that's 8, for 144, it's 256.
post your solution.
i want to see your code
i want to see your code
Watch out for the Manners Taliban!
Isn't it amazing how so many people can type "linuxpakistan.net" into their browsers but not "google.com"?
Isn't it amazing how so many people can type "linuxpakistan.net" into their browsers but not "google.com"?
Quite straightfoward. This is a bash shell example :
Code: Select all
#! /bin/bash
echo "Enter your number (positive integer): "
read number
i=1
while true
do
let "z=2**$i"
if [ $z -gt $number ]; then
echo "$z is the number - 2 to the power of $i"
exit
fi
let "i+=1"
done
Same thing in perl :
Code: Select all
#! /usr/bin/perl
print "Enter your number (positive integer): ";
chomp ($number = <>);
$i=1;
while (1) {
$z = 2 ** $i;
if ( $z > $number ) {
print "The number is $z - 2 to the power of $i\n";
last;
}
$i++;
}