- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Golang program to find the area of a rectangle
This tutorial will discuss how to find the area of a rectangle in Golang programming using two methods −
Area of the rectangle using length and breadth
Area of the rectangle using diagonal and breadth
Rectangle
A rectangle is a two-dimensional shape that has four sides. The opposite sides of a rectangle are equal and all the angles of a rectangle are at 90°. Another property of a rectangle is that its opposite sides are parallel to each other.
Area of a rectangle
The total space enclosed within the boundary of the rectangle is known as the area of a rectangle.
Its area can be calculated by multiplying the length and breadth of the rectangle.
Finding the area of a rectangle using length and breadth
Given length ‘l’ and breadth ‘b’ of a rectangle, we need to find out the area of a rectangle.
Formula
$$\mathrm{Area \, =\, Length * Breadth}$$
Example
Length= 5
Breadth= 2
Area= 5 * 2 = 10
Since, area is calculated by multiplying length and breadth, therefore we multiply 5 and 2, and obtain the area as 10.
Length= 25.5
Breadth= 5.0
Area= 127.5
Area is calculated by multiplying length and breadth, therefore −
Area= 25.5 * 5.0
= 127.5
Algorithm
Step 1 − Declare two variables- one for storing length ‘l’ and the other one for storing breadth ‘b’ of the rectangle.
Step 2 − Declare a variable for storing the area of the rectangle- ‘area’.
Step 3 − Calculate the area by multiplying the length ‘l’ and breadth ‘b’ of the rectangle and store it in the ‘area’ variable.
Step 4 − Print the calculated area, i.e, the value stored in the variable ‘area’.
Using integer variables
Example
package main // fmt package allows us to print formatted strings import "fmt" // function to calculate area of the rectangle using int variable func calculateAreaOfRectangle(l, b int) { // integer type variable ‘area’ to store area of the rectangle var area int // calculating area by multiplying length ‘l’ and breadth ‘b’ // of the rectangle area = l * b fmt.Println("Length ‘l’ =",l) fmt.Println("Breadth ‘b’ =",b) // printing area of the rectangle fmt.Println("Therefore, Area of Rectangle : ", area) } // driver function func main() { // declaring variables for storing length ‘l’ and breadth ‘b’ var l, b = 3, 4 // calling the calculateAreaOfRectangle function for // calculating the area of the rectangle and passing values // for length and breadth as parameters calculateAreaOfRectangle(l, b) }
Output
Length ‘l’ = 3 Breadth ‘b’ = 4 Therefore, Area of Rectangle : 12
Explanation
In the above code, area is calculated by multiplying the length and breadth of the rectangle using this formula −
area = l * b
Therefore, area= 3 * 4
area= 12
Using float variables
Example
package main // fmt package allows us to print formatted strings import "fmt" // function to calculate the area of the rectangle using float variable func calculateAreaOfRectangle(l, b float32) { // float type variable ‘area’ to store area of the rectangle // float32 variable stores smaller value // float64 variable stores larger value var area float32 // calculating area by multiplying length ‘l’ and breadth ‘b’ // of the rectangle area = l * b fmt.Println("Length ‘l’ =",l) fmt.Println("Breadth ‘b’ =",b) // printing area of the rectangle fmt.Println("Therefore, Area of Rectangle : ", area) } // driver function func main() { // declaring variables for storing length ‘l’ and breadth ‘b’ var l, b float32 = 5.5, 8.5 // calling the calculateAreaOfRectangle function for // calculating the area of the rectangle and passing values // for length and breadth as parameters calculateAreaOfRectangle(l, b) }
Output
Length ‘l’ = 5.5 Breadth ‘b’ = 8.5 Therefore, Area of Rectangle : 46.75
Explanation
area= 5.5 * 8.5
= 46.75
Finding the area of a rectangle using diagonal and breadth
A rectangle has two diagonals and both are equal in size. The area of a rectangle can be calculated using diagonal and breadth.
Formula
$$\mathrm{Area = Breadth \ast (\sqrt{((Diagonal)^{2} - (Breadth)^{2}))}}$$
Example
Diagonal= 30.0
Breadth= 10.0
Area= 282.842712474619 For calculating the area we are using the values of diagonal and breadth in this formula-
Area = Breadth * (√((Diagonal)2 - (Breadth)2))
Therefore, Area = 10 * (√((30)2 - (10)2))
Area = 282.842712474619
Example
package main // fmt package allows us to print formatted strings // math package allows us to perform various mathematical // operations import ( "fmt" "math" ) // function to calculate area of rectangle using float variables func calculateAreaOfRectangle(d, b float64) { // float64 type variable ‘area’ to store area of rectangle var area float64 // calculating area of the rectangle // math.Sqrt function calculates square root // math.Pow function calculates the power fmt.Println("Diagonal ‘d’ =", d) fmt.Println("Breadth ‘b’ =", b) // calculating area area = b * math.Sqrt((math.Pow(d, 2) - math.Pow(b, 2))) // printing area of the rectangle fmt.Println("Therefore, Area of Rectangle : ", area) } // driver function func main() { // declaring variables for storing diagonal ‘d’ and breadth ‘b’ var d, b float64 = 40.0, 20.0 // calling the calculateAreaOfRectangle function for // calculating the area of the rectangle and passing values // for diagonal and breadth as parameters calculateAreaOfRectangle(d, b) }
Output
Diagonal ‘d’ = 40 Breadth ‘b’ = 20 Therefore, Area of Rectangle : 692.820323027551
Explanation
For calculating the area we are using the values of diagonal and breadth in this formula −
Area = Breadth * (√((Diagonal)2 - (Breadth)2))
Therefore, Area = 20 * (√((40)2 - (20)2))
Area = 692.820323027551
Conclusion
This is all about calculating the area of a rectangle using two methods- length and breadth, and diagonal and breadth. We have also discussed using two different data types as input in this article, i.e., integer and float.
You can explore more about Golang programming using these tutorials.
- Related Articles
- Golang Program to Find the Area of a Rectangle Using Classes
- Java program to find the area of a rectangle
- Haskell Program to Find the Area of a Rectangle
- Swift Program to find the area of the rectangle
- Python Program to Find the Area of a Rectangle Using Classes
- JavaScript program to find Area and Perimeter of Rectangle
- How to find the Perimeter of a Rectangle in Golang?
- Golang program to calculate the area of a cube
- Golang Program to Find the Area of a Triangle Given All Three Sides
- Program to find largest rectangle area under histogram in python
- How to find the Area of a Circle in Golang?
- How To Find The Area of a Parallelogram in Golang?
- How To Find The Area of a Trapezium in Golang?
- Golang program to calculate the volume and area of a Cone
- Golang program to calculate the volume and area of a Sphere
