The Wayback Machine - https://web.archive.org/web/20241111072515/https://www.geeksforgeeks.org/find-surface-area-3d-figure/
Open In App

Find the Surface area of a 3D figure

Last Updated : 11 Oct, 2022
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Given a N*M matrix A[][] representing a 3D figure. The height of the building at (i, j)      is A[i][j]      . Find the surface area of the figure.

Examples : 

Input : N = 1, M = 1   A[][] = { {1} }
Output : 6

Explanation : 
The total surface area is 6 i.e 6 side of 
the figure and each are of height 1.

Input : N = 3, M = 3   A[][] = { {1, 3, 4},
                                 {2, 2, 3},
                                 {1, 2, 4} }
Output : 60

Approach : To find the surface area we need to consider the contribution of all the six sides of the given 3D figure. We will solve the questions in part to make it easy. The base of the Figure will always contribute N*M to the total surface area of the figure, and same N*M area will be contributed by the top of the figure. Now, to calculate the area contributed by the walls, we will take out the absolute difference between the height of two adjacent wall. The difference will be the contribution in the total surface area.

Below is the implementation of the above idea : 

C++

// CPP program to find the Surface area of a 3D figure
#include <bits/stdc++.h>
using namespace std;
 
// Declaring the size of the matrix
const int M = 3;
const int N = 3;
 
// Absolute Difference between the height of
// two consecutive blocks
int contribution_height(int current, int previous)
{
    return abs(current - previous);
}
 
// Function To calculate the Total surfaceArea.
int surfaceArea(int A[N][M])
{
    int ans = 0;
 
    // Traversing the matrix.
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
 
            /* If we are traveling the topmost row in the
            matrix, we declare the wall above it as 0
            as there is no wall above it. */
            int up = 0;
 
            /* If we are traveling the leftmost column in the
            matrix, we declare the wall left to it as 0
            as there is no wall left it. */
            int left = 0;
 
            // If its not the topmost row
            if (i > 0)
                up = A[i - 1][j];
 
            // If its not the leftmost column
            if (j > 0)
                left = A[i][j - 1];
 
            // Summing up the contribution of by
            // the current block
            ans += contribution_height(A[i][j], up)
                    + contribution_height(A[i][j], left);
 
            /* If its the rightmost block of the matrix
               it will contribute area equal to its height
               as a wall on the right of the figure */
            if (i == N - 1)
                ans += A[i][j];
 
            /* If its the lowest block of the matrix it will
               contribute area equal to its height as a wall
               on the bottom of the figure */
            if (j == M - 1)
                ans += A[i][j];
        }
    }
 
    // Adding the contribution by the base and top of the figure
    ans += N * M * 2;
    return ans;
}
 
// Driver program
int main()
{
    int A[N][M] = { { 1, 3, 4 },
                    { 2, 2, 3 },
                    { 1, 2, 4 } };
    cout << surfaceArea(A) << endl;
    return 0;
}

                    

Java

// Java program to find the Surface
// area of a 3D figure
 
class GFG
{
    // Declaring the size of the matrix
    static final int M=3;
    static final int N=3;
     
    // Absolute Difference between the height of
    // two consecutive blocks
    static int contribution_height(int current, int previous)
    {
        return Math.abs(current - previous);
    }
     
    // Function To calculate the Total surfaceArea.
    static int surfaceArea(int A[][])
    {
        int ans = 0;
     
        // Traversing the matrix.
        for (int i = 0; i < N; i++)
        {
            for (int j = 0; j < M; j++) {
     
                /* If we are traveling the topmost
                row in the matrix, we declare the
                wall above it as 0 as there is no
                wall above it. */
                int up = 0;
     
                /* If we are traveling the leftmost
                column in the matrix, we declare the
                wall left to it as 0as there is no
                wall left it. */
                int left = 0;
     
                // If its not the topmost row
                if (i > 0)
                    up = A[i - 1][j];
     
                // If its not the leftmost column
                if (j > 0)
                    left = A[i][j - 1];
     
                // Summing up the contribution of by
                // the current block
                ans += contribution_height(A[i][j], up)
                       + contribution_height(A[i][j], left);
     
                /* If its the rightmost block of the matrix
                it will contribute area equal to its height
                as a wall on the right of the figure */
                if (i == N - 1)
                    ans += A[i][j];
     
                /* If its the lowest block of the
                matrix it will contribute area equal
                to its height as a wall on
                 the bottom of the figure */
                if (j == M - 1)
                    ans += A[i][j];
            }
        }
     
        // Adding the contribution by
        // the base and top of the figure
        ans += N * M * 2;
        return ans;
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int A[][] = {{ 1, 3, 4 },
                     { 2, 2, 3 },
                     { 1, 2, 4 } };
        System.out.println(surfaceArea(A));
    }
}
 
// This code is contributed By Anant Agarwal.

                    

Python3

# Python3 program to find the
# Surface area of a 3D figure
 
 
# Declaring the size
# of the matrix
M = 3;
N = 3;
 
# Absolute Difference
# between the height of
# two consecutive blocks
def contribution_height(current, previous):
    return abs(current - previous);
 
# Function To calculate
# the Total surfaceArea.
def surfaceArea(A):
    ans = 0;
 
    # Traversing the matrix.
    for i in range(N):
        for j in range(M):
 
            # If we are traveling the
            # topmost row in the matrix,
            # we declare the wall above it
            # as 0 as there is no wall
            # above it.
            up = 0;
 
            # If we are traveling the
            # leftmost column in the
            # matrix, we declare the wall
            # left to it as 0 as there is
            # no wall left it.
            left = 0;
 
            # If its not the topmost row
            if (i > 0):
                up = A[i - 1][j];
 
            # If its not the
            # leftmost column
            if (j > 0):
                left = A[i][j - 1];
 
            # Summing up the
            # contribution of by
            # the current block
            ans += contribution_height(A[i][j], up)+contribution_height(A[i][j], left);
             
            # If its the rightmost block
            # of the matrix it will contribute
            # area equal to its height as a
            # wall on the right of the figure */
            if (i == N - 1):
                ans += A[i][j];
 
            # If its the lowest block
            # of the matrix it will
            # contribute area equal to
            # its height as a wall on
            # the bottom of the figure
            if (j == M - 1):
                ans += A[i][j];
 
    # Adding the contribution by
    # the base and top of the figure
    ans += N * M * 2;
    return ans;
 
# Driver Code
A = [[1, 3, 4],[2, 2, 3],[1, 2, 4]];
print(surfaceArea(A));
 
# This code is contributed By mits

                    

C#

// C# program to find the
// Surface area of a 3D figure
using System;
 
class GFG
{
    // Declaring the size of the matrix
    static int M=3;
    static int N=3;
     
    // Absolute Difference between the
    // height of two consecutive blocks
    static int contribution_height(int current, int previous)
    {
        return Math.Abs(current - previous);
    }
     
    // Function To calculate the
    // Total surfaceArea.
    static int surfaceArea(int [,]A)
    {
        int ans = 0;
     
    // Traversing the matrix.
        for (int i = 0; i < N; i++)
        {
            for (int j = 0; j < M; j++) {
     
    // If we are traveling the topmost
    // row in the matrix, we declare the
    // wall above it as 0 as there is no
    // wall above it.
                int up = 0;
     
    // If we are traveling the leftmost
    // column in the matrix, we declare
    // the wall left to it as 0as there
    // is no wall left it.
                int left = 0;
     
    // If its not the topmost row
                if (i > 0)
                    up = A[i - 1,j];
     
    // If its not the leftmost column
                if (j > 0)
                    left = A[i,j - 1];
     
    // Summing up the contribution 
    // of by the current block
            ans += contribution_height(A[i,j], up)
                + contribution_height(A[i,j], left);
     
    // If its the rightmost block of the
    // matrix it will contribute area equal
    // to its height as a wall on the right
    // of the figure
                if (i == N - 1)
                    ans += A[i,j];
     
    // If its the lowest block of the
    // matrix it will contribute area 
    // equal to its height as a wall
    // on the bottom of the figure
                if (j == M - 1)
                    ans += A[i,j];
            }
        }
     
    // Adding the contribution by the
    // base and top of the figure
        ans += N * M * 2;
        return ans;
    }
     
    // Driver code
    public static void Main ()
    {
        int [,]A = {{ 1, 3, 4 },
                    { 2, 2, 3 },
                    { 1, 2, 4 } };
        Console.WriteLine(surfaceArea(A));
    }
}
 
// This code is contributed By vt_m.

                    

PHP

<?php
// PHP program to find the
// Surface area of a 3D figure
 
 
// Declaring the size
// of the matrix
$M = 3;
$N = 3;
 
// Absolute Difference
// between the height of
// two consecutive blocks
function contribution_height($current,
                             $previous)
{
    return abs($current - $previous);
}
 
// Function To calculate
// the Total surfaceArea.
function surfaceArea($A)
{
    global $M;
    global $N;
    $ans = 0;
 
    // Traversing the matrix.
    for ($i = 0; $i < $N; $i++)
    {
        for ($j = 0; $j < $M; $j++)
        {
 
            /* If we are traveling the
            topmost row in the matrix,
            we declare the wall above it
            as 0 as there is no wall
            above it. */
            $up = 0;
 
            /* If we are traveling the
            leftmost column in the
            matrix, we declare the wall
            left to it as 0 as there is
            no wall left it. */
            $left = 0;
 
            // If its not the topmost row
            if ($i > 0)
                $up = $A[$i - 1][$j];
 
            // If its not the
            // leftmost column
            if ($j > 0)
                $left = $A[$i][$j - 1];
 
            // Summing up the
            // contribution of by
            // the current block
            $ans += contribution_height($A[$i][$j], $up) +
                    contribution_height($A[$i][$j], $left);
             
            /* If its the rightmost block
            of the matrix it will contribute
            area equal to its height as a
            wall on the right of the figure */
            if ($i == $N - 1)
                $ans += $A[$i][$j];
 
            /* If its the lowest block
               of the matrix it will
               contribute area equal to
               its height as a wall on
               the bottom of the figure */
            if ($j == $M - 1)
                $ans += $A[$i][$j];
        }
    }
 
    // Adding the contribution by
    // the base and top of the figure
    $ans += $N * $M * 2;
    return $ans;
}
 
// Driver Code
$A = array(array(1, 3, 4),
           array(2, 2, 3),
           array(1, 2, 4));
echo surfaceArea($A);
 
// This code is contributed By mits
?>

                    

Javascript

<script>
 
// JavaScript program to find the Surface
// area of a 3D figure
 
// Declaring the size of the matrix
    let M=3;
    let N=3;
       
    // Absolute Difference between the height of
    // two consecutive blocks
    function contribution_height(current, previous)
    {
        return Math.abs(current - previous);
    }
       
    // Function To calculate the Total surfaceArea.
    function surfaceArea( A)
    {
        let ans = 0;
       
        // Traversing the matrix.
        for (let i = 0; i < N; i++)
        {
            for (let j = 0; j < M; j++) {
       
                /* If we are traveling the topmost
                row in the matrix, we declare the
                wall above it as 0 as there is no
                wall above it. */
                let up = 0;
       
                /* If we are traveling the leftmost
                column in the matrix, we declare the
                wall left to it as 0as there is no
                wall left it. */
                let left = 0;
       
                // If its not the topmost row
                if (i > 0)
                    up = A[i - 1][j];
       
                // If its not the leftmost column
                if (j > 0)
                    left = A[i][j - 1];
       
                // Summing up the contribution of by
                // the current block
                ans += contribution_height(A[i][j], up)
                       + contribution_height(A[i][j], left);
       
                /* If its the rightmost block of the matrix
                it will contribute area equal to its height
                as a wall on the right of the figure */
                if (i == N - 1)
                    ans += A[i][j];
       
                /* If its the lowest block of the
                matrix it will contribute area equal
                to its height as a wall on
                 the bottom of the figure */
                if (j == M - 1)
                    ans += A[i][j];
            }
        }
       
        // Adding the contribution by
        // the base and top of the figure
        ans += N * M * 2;
        return ans;
    }
 
// Driver code
         
        let A = [[ 1, 3, 4 ],
                     [ 2, 2, 3 ],
                     [ 1, 2, 4 ]];
        document.write(surfaceArea(A));
                   
</script>

                    

Output
60

Time Complexity: O(N*M), as the above code is been iterating over through two loops. 
Auxiliary Space: O(1)



Previous Article
Next Article

Similar Reads

Calculate Volume, Curved Surface Area and Total Surface Area Of Cylinder
Given radius and height of Cylinder, calculate the volume, total surface area and curved surface area of cylinder. Volume of Cylinder: The volume of cylinder is defined as the amount of three dimensional space occupied by the cylinder or the storage capacity of a cylinder. We can calculate volume of cylinder by using formula: where ' r 'is radius o
7 min read
Program to find the surface area of the square pyramid
Given the base length(b) and slant height(s) of the square pyramid. The task is to find the surface area of the Square Pyramid. A Pyramid with a square base, 4 triangular faces, and an apex is a square pyramid. In this figure, b – base length of the square pyramid. s – slant height of the square pyramid. h – height of the square pyramid.Examples: I
3 min read
Program to find volume and surface area of pentagonal prism
A prism that has 5 rectangular faces and 2 parallel pentagonal bases is a pentagonal prism. So you are given the apothem length(a), base length(b) and height(h) of the pentagonal prism. you have to find the surface area and the volume of the Pentagonal Prism.Examples: Input : a=3, b=5, h=6 Output :surface area=225, volume=225 Input : a=2, b=3, h=5
4 min read
Program to find Surface Area and Volume of Octagonal Prism
Given the base length, height, and distance of Octagonal Prism we have to find the Surface Area and Volume of the octagonal prism for the given parameters. The octagonal prism is a solid three-dimensional object having a total of 10 faces, 24 edges, and 16 vertices. The two faces at either end are octagons and the rest of the faces are rectangular.
5 min read
Calculate Volume and Surface area Of Sphere
Given radius of sphere, calculate the volume and surface area of sphere.Sphere: Just like a circle, which geometrically is a two-dimensional object, a sphere is defined mathematically as the set of points that are all at the same distance r from a given point, but in three-dimensional space. This distance r is the radius of the sphere, and the give
6 min read
Program for Surface Area of Octahedron
Given the sides of Octahedron then calculate the surface area. Examples: Input : 7 Output : 169.741 Input : 9 Output : 280.59 An regular octahedron has eight faces, which are all in the shape of equilateral triangles.The area of an octahedron is 2 multiplied by the length of an edge squared multiplied by the square root of three. Formula: Surface a
2 min read
Program for Surface area of Dodecahedron
Given the edge of the dodecahedron calculate its surface area. Surface area is the amount of space that all the faces of the shape take up.Formula: [Tex]\mathbf{A=3\sqrt{25+10\sqrt{5}}\hspace{2 mm} a^{2}} [/Tex]Examples: Input: 3 Output: 185.812 Input: 7 Output: 1011.64 A dodecahedron is a 3-dimensional figure made up of 12 faces, or flat sides. Al
3 min read
Program to calculate the Surface Area of a Triangular Prism
In mathematics, a triangular prism is a three-dimensional solid shape with two identical ends connected by equal parallel lines, and have 5 faces, 9 edges, and 6 vertices. where "b" is the length of the base, "h" is the height of the triangle, "s1, s2, s3" are the respective length of each side of the triangle, and H is the height of the prism (whi
5 min read
Surface Area and Volume of Hexagonal Prism
Given a Base edge and Height of the Hexagonal prism, the task is to find the Surface Area and the Volume of hexagonal Prism. In mathematics, a hexagonal prism is a three-dimensional solid shape which have 8 faces, 18 edges, and 12 vertices. The two faces at either ends are hexagons, and the rest of the faces of the hexagonal prism are rectangular.
5 min read
Calculate volume and surface area of Torus
This article is about the surface and mathematical concept of a torus. A 3D shape made by revolving a small circle (radius r) along a line made by a bigger circle (radius R). Property: It can be made by revolving a small circle (radius r) along a line made by a bigger circle (radius R).It is not a polyhedronIt has no vertices or edgesSurface Area T
5 min read
Python | Percentage increase in the total surface area of the cuboid
Given a cuboid of length L, breadth B and Height H, the task is to find percentage increase in the total surface area of the cuboid if length, breadth and height are increased by fixed percentages. Examples: Input : L = 20, B = 30, H = 50, l = 10 %, b = 12 %, h = 15 % Output : 26.97 % Input : L = 40, B = 60, H = 15, l = 5 %, b = 7 %, h = 12 % Outpu
2 min read
Class 8 RD Sharma Solutions- Chapter 22 Mensuration III (Surface Area And Volume Of Right Circular Cylinder) - Exercise 22.2 | Set 1
Question 1. Find the volume of a cylinder whose (i) r = 3.5 cm, h = 40 cm (ii) r = 2.8 m, h = 15 m Solution: (i) Given that, r = 3.5 cm, h = 40 cm As we know that Volume of a cylinder = πr2h = 22/7 × 3.5 × 3.5 × 40 = 1540 cm3 (ii) Given that, r = 2.8 m, h =15 m As we know that Volume of a cylinder = πr2h = 22/7 × 2.8 × 2.8 × 15 = 369.6 m3 Question
11 min read
Class 9 RD Sharma Solutions - Chapter 19 Surface Area And Volume of a Right Circular Cylinder - Exercise 19.2 | Set 2
Question 17. The height of a right circular cylinder is 10.5 m. Three times the sum of the areas of its two circular faces is twice the area of the curved surface. Find the volume of the cylinder. Solution: Given that Height of the cylinder = 10.5 m According to the question 3(2πr2) = 2(2πrh) 3r = 2h r = 2/3 x h r = 2/3 x 10.5 = 7cm So, the radius
14 min read
Program to calculate Surface Area of Ellipsoid
Given the length of the three semi-axes as A, B, and C, the task is to find the surface area of the given Ellipsoid. Ellipsoid is a closed surface of which all plane cross-sections are either ellipses or circles. An ellipsoid is symmetrical about the three mutually perpendicular axes that intersect at the center. It is a three-dimensional, closed g
4 min read
Volume and Surface Area - Aptitude Question and Answer
Volume and Surface area topics are very crucial topics for any exam that has Quantitative Aptitude section in their exam pattern. One such National exam is SSC CGL, Every year you can witness at least one question from this topic and as we all know, in any government exam, even a single mark can make a huge difference in ranking or clearing the cut
8 min read
Program for Volume and Surface Area of Cube
Cube is a 3-dimensional box-like figure represented in the 3-dimensional plane. Cube has 6 squared-shape equal faces. Each face meet another face at 90 degree each. Three sides of cube meet at same vertex. Examples: Input : Side of a cube = 2 Output : Area = 8 Total surface area = 24 Input : Side of a cube = 3 Output : Area = 27 Total surface area
3 min read
Program for Volume and Surface Area of Cuboid
Cuboid is a 3-dimensional box-like figure represented in the 3-dimensional plane.Cuboid has 6 rectangled-shape faces. Each face meet another face at 90 degree each.Three sides of cuboid meet at same vertex.Since it is made up of 6 rectangle faces, it have length, width and height of different dimension. Examples : Input : 2 3 4 Output : Area = 24 T
4 min read
Program to calculate Volume and Surface area of Hemisphere
Calculate volume and surface area of a hemisphere. Hemisphere : In geometry, it is an exact half of a sphere. We can find many of the real life examples of the hemispheres such as our planet Earth can be divided into two hemisphere the southern & northern hemispheres.   Volume of Hemisphere : Volume of a Hemisphere is nothing but the amount of
4 min read
Calculate volume and surface area of a cone
Given slant height, height and radius of a cone, we have to calculate the volume and surface area of the cone.  Cone : Cone is a three dimensional geometric shape. It consists of a base having the shape of a circle and a curved side (the lateral surface) ending up in a tip called the apex or vertex.     Volume of a cone : The volume of a cone is gi
5 min read
Program for Volume and Surface area of Frustum of Cone
Given slant height, height and radius of a frustum of a cone, we have to calculate the volume and surface area of the frustum of a cone. Frustum of cone In geometry, a frustum is the portion of a solid (normally a cone or pyramid) that lies between one or two parallel planes cutting it. If we cut a right circular cone by a plane parallel to its bas
8 min read
Class 9 RD Sharma Solutions - Chapter 19 Surface Area And Volume of a Right Circular Cylinder - Exercise 19.2 | Set 1
This geometric figure is known as Right Circular Cylinder, which has ample usages in day to day real life situation arises in Engineering, Architecture and Manufacture. It is well used in areas of estimations concerning materials as well as design works concerning the physical structure of an object. Area of regression: right circular cylinderThis
14 min read
Surface Area of a Solid of Revolution
Surface Area of a Solid of Revolution: The area of the surface of a solid of revolution is a fundamental concept in calculus and engineering. It involves rotating a curve around an axis to form a three-dimensional solid and then determining the surface area of this solid. This concept has significant applications in various fields, including physic
7 min read
Find area of the larger circle when radius of the smaller circle and difference in the area is given
Given two integers r and d where r is the radius of the smaller circle and d is the difference of the area of this circle with some larger radius circle. The task is to find the area of the larger circle.Examples: Input: r = 4, d = 5 Output: 55.24 Area of the smaller circle = 3.14 * 4 * 4 = 50.24 55.24 - 50.24 = 5Input: r = 12, d = 3 Output: 455.16
4 min read
Find area of the Circle when the area of inscribed Square is given
Given the area of a square inscribed in a circle as N, the task is to calculate the area of a circle in which the square is inscribed. Examples: Input: N = 4 Output: 6.283 Input: N = 10Output: 15.707 Approach: Consider the below image: Let the area of the square is 'A'The side of the square is given by = A**(1/2)A right-angled triangle is formed by
4 min read
Number of cells in the Nth order figure
Given an integer N, the task is to find the number of cells in Nth order figure of the given type: Examples: Input: N = 2 Output: 5Input: N = 3 Output: 13 Approach: It can be observed that for the values of N = 1, 2, 3, ... a series will be formed as 1, 5, 13, 25, 41, 61, 85, 113, 145, 181, ... whose Nth term will be N2 + (N - 1)2.Below is the impl
3 min read
Radius of a circle having area equal to the sum of area of the circles having given radii
Given two integers r1 and r2, representing the radius of two circles, the task is to find the radius of the circle having area equal to the sum of the area of the two circles having given radii. Examples: Input:r1 = 8r2 = 6Output:10Explanation:Area of circle with radius 8 = 201.061929 Area of a circle with radius 6 = 113.097335Area of a circle with
5 min read
Check if an area P can be obtained from an area of N * M
Given integers N, M and P, the task is to check if is possible to find a rectangular area of P sq. units inside a rectangular area of N × M sq units. Examples: Input: N = 3, M = 3, P = 4Output: YESExplanation: Rectangle of 2 x 2 sq. unit area Input: N = 4, M = 4, P = 7Output: NO Approach: Follow the steps below to solve the problem Find all the fac
6 min read
Class 10 RD Sharma Solutions - Chapter 16 Surface Areas and Volumes - Exercise 16.2 | Set 2
Chapter 16 of RD Sharma's Class 10 textbook "Surface Areas and Volumes" explores the mathematical principles used to calculate the surface areas and volumes of the various geometric shapes. This chapter is crucial for understanding how to measure and interpret the size of three-dimensional objects which has practical applications in fields such as
13 min read
Class 10 RD Sharma Solutions - Chapter 16 Surface Areas and Volumes - Exercise 16.2 | Set 1
Question 1. Consider a tent cylindrical in shape and surmounted by a conical top having a height of 16 m and a radius as common for all the surfaces constituting the whole portion of the tent which is equal to 24 m. The height of the cylindrical portion of the tent is 11 m. Find the area of Canvas required for the tent.Solution:  According to the q
14 min read
Program to find area of a Trapezoid
Definition of Trapezoid : A Trapezoid is a convex quadrilateral with at least one pair of parallel sides. The parallel sides are called the bases of the trapezoid and the other two sides which are not parallel are referred to as the legs. There can also be two pairs of bases. In the above figure CD || AB, so they form the bases and the other two si
4 min read
three90RightbarBannerImg