w3resource

Ruby Basic Exercises: Compute the parameter and area

Ruby Basic: Exercise-4 with Solution

Write a Ruby program which accept the radius of a circle from the user and compute the parameter and area.

Ruby Basic Exercises: Compute the parameter and area

Ruby Code:

radius = 5.0
perimeter = 0.0
area = 0.0
print "Input the radius of the circle: "
radius = gets.to_f
perimeter = 2 * 3.141592653 * radius
area = 3.141592653 * radius * radius
puts "The perimeter is #{perimeter}."
puts "The area is #{area}."

Output:

Input the radius of the circle: The perimeter is 31.41592653.
The area is 78.539816325.

Flowchart:

Flowchart: Compute the parameter and area

Ruby Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Ruby program to create a new string which is n copies of a given string where n is a non-negative integer.
Next: Write a Ruby program to check if a string starts with "if".

What is the difficulty level of this exercise?