Ruby program to check a number is zero or not using the zero?() function

Ruby Example: Write a program to check a number is zero or not using the zero?() function.
Submitted by Nidhi, on December 13, 2021

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 »




Comments and Discussions!

Load comments ↻





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