Home »
Ruby Tutorial »
Ruby Programs
Ruby program to check a number is zero or not using the zero?() function
Last Updated : December 15, 2025
Problem Solution
In this program, we will create 3 variables with some initial value. Then we will check given number is zero or not using library function zero?().
Program/Source Code
The source code to check a number is zero or not using zero?() the function is given below. The given program is compiled and executed successfully.
# Ruby program to check a number
# is zero or not using the zero?()
# function
num1 = 0;
num2 = Complex(0,0);
num3 = Complex(2,0);
print "Num1: " ,num1.zero?();
print "\nNum2: ",num2.zero?();
print "\nNum3: ",num3.zero?();
Output
Num1: true
Num2: true
Num3: false
Explanation
In the above program, we created three numbers with some initial values and checked the created variable contains value zero or not using zero?() function. The zero?() function returns Boolean values true or false based on the value of the variable is zero or not. After that, we printed the result.
Ruby Basic Programs »
Advertisement
Advertisement