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.
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.
Output:
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.
need an explanation for this answer? contact us directly to get an explanation for this answer