Q:

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

belongs to collection: Ruby Basic Programs

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Ruby Basic Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Ruby program to read the height of a person and th... >>
<< Ruby program to read coordinate points and determi...