Ruby program to demonstrate the next() function

Ruby Example: Write a program to demonstrate the next() function.
Submitted by Nidhi, on December 16, 2021

Problem Solution:

In this program, we will create 4 integer variables with some initial value. Then we will use the next() function to get the next integer value. The next() function returns the 1 incremented value of the given number.

Program/Source Code:

The source code to demonstrate the next() function is given below. The given program is compiled and executed successfully.

# Ruby program to demonstrate 
# the next() function

num1 = 10;
num2 = -10;
num3 = 15;
num4 = -15;

print "Num1: ",num1.next(),"\n";
print "Num2: ",num2.next(),"\n";
print "Num3: ",num3.next(),"\n";
print "Num4: ",num4.next(),"\n";

Output:

Num1: 11
Num2: -9
Num3: 16
Num4: -14

Explanation:

In the above program, we created num1, num2, num3, num4 that are initialized with 10, -10, 15, -15. Then we called the next() function to get the number incremented by 1 and printed the result.

Ruby Basic Programs »




Comments and Discussions!

Load comments ↻





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