×

Ruby Tutorial

Ruby Basics

Ruby Control Statements

Ruby Methods

Ruby Classes and Methods

Ruby Arrays

Ruby Sets

Ruby Strings

Ruby Classes & Objects

Ruby Hash

Ruby Tools

Ruby Functions

Ruby Built-in Functions

Misc.

Ruby Programs

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

Last Updated : December 15, 2025

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 »


Advertisement
Advertisement


Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

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