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

Ruby Example: Write a program to calculate the base 2 logarithm of the given value.
Submitted by Nidhi, on December 06, 2021

Problem Solution:

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.

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.

Ruby Basic Programs »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.