Q:

Ruby program to calculate the base 2 logarithm of the given value

0

In this program, we will read an integer number from the user using gets.chomp.to_i. Then we will calculate the base 2 logarithm of a given number using the Math.log2() function.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program/Source Code:

The source code to calculate the base 2 logarithm of the given number is given below. The given program is compiled and executed successfully.

# Ruby program to calculate the 
# base 2 logarithm of the given value

print "Enter value: ";
num = gets.chomp.to_i;  

Log2 = Math.log2(num);

print "Base 2 logarithm is: ",Log2;

Output:

Enter value: 213
Base 2 logarithm is: 7.734709620225838

Explanation:

In the above program, we read an integer number from the user using gets.chomp.to_i and calculated the base 2 logarithm of input number using Math.log2() function printed the result.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now