w3resource

Ruby Basic Exercises: Retrieve the student information from a hash

Ruby Basic: Exercise-31 with Solution

Write a Ruby program to retrieve the total marks where subject name and marks of a student stored in a hash.

Sample subject and marks : Literature -74, Science – 89, Math-91

Ruby Code:

student_marks = Hash.new 0
student_marks['Literature'] = 74
student_marks['Science'] = 89
student_marks['Math'] = 91
total_marks = 0
student_marks.each {|key,value|
              total_marks +=value
        } 
puts "Total Marks: "+total_marks.to_s

Output:

Total Marks: 254

Flowchart:

Flowchart: Retrieve the student information from a hash

Ruby Code Editor:

Contribute your code and comments through Disqus.

Previous:Write a Ruby program to check two non-negative integer values and return true if they have the same last digit.
Next: Write a Ruby program to print a specified character twenty times.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/ruby-exercises/basic/ruby-basic-exercise-31.php