×

Ruby Tutorial

Ruby Basics

Ruby Control Statements

Ruby Methods

Ruby Classes and Methods

Ruby Arrays

Ruby Sets

Ruby Strings

Ruby Classes & Objects

Ruby Hash

Ruby Tools

Ruby Functions

Ruby Built-in Functions

Misc.

Ruby Programs

Ruby program to calculate the area of Trapezium

Last Updated : December 15, 2025

Problem Solution

In this program, we will read base1, base2, and height from the user and then we will calculate the area of Trapezium and print the result.

Program/Source Code

The source code to calculate the area of Trapezium is given below. The given program is compiled and executed successfully.

# Ruby program to calculate the 
# area of Trapezium

base1=0
base2=0
height=0
area=0

print "Enter base1: ";
base1 = gets.chomp.to_i;  

print "Enter base2: ";
base2 = gets.chomp.to_i;  

print "Enter height: ";
height = gets.chomp.to_i;

area = 0.5 * (base1 + base2) * height;
    
print "Area of Trapezium is: ",area;

Output

Enter base1: 3
Enter base2: 4
Enter height: 2
Area of Trapezium is: 7.0

Explanation

In the above program, we created four variables base1, base2, height, area initialized with 0. Then we read values base1, base2, height from the user. After that, we calculated the area of the Trapezium and printed the result.

Ruby Basic Programs »


Advertisement
Advertisement


Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.