Home »
Ruby »
Ruby Programs
Ruby program to print ASCII value of given character
Ruby Example: Write a program to print ASCII value of given character.
Submitted by Nidhi, on December 12, 2021
Problem Solution:
In this program, we will create some character variables and print the corresponding ASCII values using the ord() function.
Program/Source Code:
The source code to print the ASCII value of a given character is given below. The given program is compiled and executed successfully.
# Ruby program to print ASCII value
# of given character
ch1 = 'A';
ch2 = 'C';
ch3 = 'a';
ch4 = 'd';
print "ASCII value of 'ch1' is: ",ch1.ord(),"\n";
print "ASCII value of 'ch2' is: ",ch2.ord(),"\n";
print "ASCII value of 'ch3' is: ",ch3.ord(),"\n";
print "ASCII value of 'ch4' is: ",ch4.ord(),"\n";
Output:
ASCII value of 'ch1' is: 65
ASCII value of 'ch2' is: 67
ASCII value of 'ch3' is: 97
ASCII value of 'ch4' is: 100
Explanation:
In the above program, we created 4 variables ch1, ch2, ch3, ch4 initialized with 'A', 'C', 'a', 'D' respectively. Then we got the ASCII value of characters using the ord() function and printed the result.
Ruby Basic Programs »