logo image

Ruby program to calculate square area and circumference

60


Following program shows you how to calculate square area and circumference. This program gets square length from user and calculates area and circumference and prints them using following formulas Area = length X length Circumference = 4 X length

 puts "Enter length of square:"
 squareLength = gets.to_f
 areaOfSquare = squareLength * squareLength
 puts "Area of square is: #{areaOfSquare}"
 circumferenceOfSquare = 4 * squareLength
 puts "Circumference of square is: #{circumferenceOfSquare}"

Output:

Enter length of square:
5
Area of square is: 25.0
Circumference of square is: 20.0