×

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 extract the last two digits from the given year

Last Updated : December 15, 2025

Problem Solution

In this program, we will read a year from the user. Then we will extract the last two digits from a given year and assign the result to another variable.

Program/Source Code

The source code to extract the last two digits from a given year is given below. The given program is compiled and executed successfully.

# Ruby program to extract the last two digits 
# from the given year

Year = 0;
Res  = 0;

print "Enter year: ";
Year = gets.chomp.to_i;  

Res = Year % 100;

print "Result is: ", Res;

Output

Enter year: 2022
Result is: 22

Explanation

In the above program, we created two variables Year and Res,  that are initialized with 0. Then we read the value of Year from the user. Then we extracted the last 2 digits from a given year and assigned it to the Res variable. After that, we printed the value Res variable.

Ruby Basic Programs »


Advertisement
Advertisement


Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

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