Posted on by Kalkicode
Code Geometric

Find the surface area of cube

A cube is a three-dimensional solid object that has six equal square faces. Finding the surface area of a cube involves determining the total area of all its faces. This calculation is essential in various fields, including mathematics, architecture, and manufacturing, where cubes are common shapes.

Problem Statement

Given the length of an edge of the cube, the task is to calculate its surface area. The formula for finding the surface area of a cube in terms of its edge length 'e' is:

Surface Area = 6 * e^2

Example Scenario

Imagine you are a packaging designer responsible for creating the packaging for a cubic product. To ensure you have enough material to cover all the faces of the box, you need to calculate the total surface area of the cube. This calculation helps you determine the required amount of packaging material.

Idea to Solve the Problem

To solve this problem, we can follow these steps:

  1. Accept the length of an edge of the cube as input.
  2. Use the formula to calculate the surface area of the cube.
  3. Display the calculated surface area.

Pseudocode

function cube_surface_area(edge):
    area = 6 * edge * edge
    return area

main:
    edge1 = 7
    edge2 = 15
    edge3 = 4
    edge4 = 4.7
    
    area1 = cube_surface_area(edge1)
    area2 = cube_surface_area(edge2)
    area3 = cube_surface_area(edge3)
    area4 = cube_surface_area(edge4)
    
    print("Cube Size [ edge :", edge1, "]")
    print("Surface Area :", area1)
    
    print("Cube Size [ edge :", edge2, "]")
    print("Surface Area :", area2)
    
    print("Cube Size [ edge :", edge3, "]")
    print("Surface Area :", area3)
    
    print("Cube Size [ edge :", edge4, "]")
    print("Surface Area :", area4)

Algorithm Explanation

  1. Define a function cube_surface_area that takes the edge length as input.
  2. Inside the function, use the provided formula to calculate the surface area of the cube.
  3. In the main function, set four test cases with different values for the edge length.
  4. Calculate the surface areas for each test case by calling the cube_surface_area function.
  5. Display the calculated surface areas along with their respective edge lengths.

Code Solution

//C Program
//Find the surface area of cube
#include <stdio.h>

//Calculate surface area of cube by given edge
void cube_surface_area(double edge)
{
	// Formula
	// 6 * edge * edge
	double area = 6 * edge * edge;
	//Display result
	printf(" Cube Size [ edge : %lf] ", edge);
	printf("\n Surface Area : %lf\n\n", area);
}
int main()
{
	//Test Case
	cube_surface_area(7);
	cube_surface_area(15);
	cube_surface_area(4);
	cube_surface_area(4.7);
	return 0;
}

Output

 Cube Size [ edge : 7.000000]
 Surface Area : 294.000000

 Cube Size [ edge : 15.000000]
 Surface Area : 1350.000000

 Cube Size [ edge : 4.000000]
 Surface Area : 96.000000

 Cube Size [ edge : 4.700000]
 Surface Area : 132.540000
// Java Program
// Find the surface area of cube
class Cube
{
	//Calculate surface area of cube by given edge
	public void cube_surface_area(double edge)
	{
		// Formula
		// 6 * edge * edge
		double area = 6 * edge * edge;
		//Display result
		System.out.print(" Cube Size [ edge : " + edge + "] ");
		System.out.print("\n Surface Area : " + area + "\n\n");
	}
	public static void main(String[] args)
	{
		Cube obj = new Cube();
		//Test Case
		obj.cube_surface_area(7);
		obj.cube_surface_area(15);
		obj.cube_surface_area(4);
		obj.cube_surface_area(4.7);
	}
}

Output

 Cube Size [ edge : 7.0]
 Surface Area : 294.0

 Cube Size [ edge : 15.0]
 Surface Area : 1350.0

 Cube Size [ edge : 4.0]
 Surface Area : 96.0

 Cube Size [ edge : 4.7]
 Surface Area : 132.54000000000002
// C++ Program
// Find the surface area of cube
#include<iostream>

using namespace std;
class Cube
{
	public:
		//Calculate surface area of cube by given edge
		void cube_surface_area(double edge)
		{
			// Formula
			// 6 *edge *edge
			double area = 6 *edge *edge;
			cout << " Cube Size [ edge : " << edge << "] ";
			cout << "\n Surface Area : " << area << "\n\n";
		}
};
int main()
{
	Cube obj ;
	//Test Case
	obj.cube_surface_area(7);
	obj.cube_surface_area(15);
	obj.cube_surface_area(4);
	obj.cube_surface_area(4.7);
	return 0;
}

Output

 Cube Size [ edge : 7]
 Surface Area : 294

 Cube Size [ edge : 15]
 Surface Area : 1350

 Cube Size [ edge : 4]
 Surface Area : 96

 Cube Size [ edge : 4.7]
 Surface Area : 132.54
// C# Program
// Find the surface area of cube
using System;
class Cube
{
	//Calculate surface area of cube by given edge
	public void cube_surface_area(double edge)
	{
		// Formula
		// 6 * edge * edge
		double area = 6 * edge * edge;
		Console.Write(" Cube Size [ edge : " + edge + "] ");
		Console.Write("\n Surface Area : " + area + "\n\n");
	}
	public static void Main(String[] args)
	{
		Cube obj = new Cube();
		//Test Case
		obj.cube_surface_area(7);
		obj.cube_surface_area(15);
		obj.cube_surface_area(4);
		obj.cube_surface_area(4.7);
	}
}

Output

 Cube Size [ edge : 7]
 Surface Area : 294

 Cube Size [ edge : 15]
 Surface Area : 1350

 Cube Size [ edge : 4]
 Surface Area : 96

 Cube Size [ edge : 4.7]
 Surface Area : 132.54
<?php
// Php Program
// Find the surface area of cube
class Cube
{
	//Calculate surface area of cube by given edge
	public 	function cube_surface_area($edge)
	{
		// Formula
		// 6 *edge *edge
		$area = 6 *$edge *$edge;
		//Display result
		echo(" Cube Size [ edge : ". $edge ."] ");
		echo("\n Surface Area : ". $area ."\n\n");
	}
}

function main()
{
	$obj = new Cube();
	//Test Case
	$obj->cube_surface_area(7);
	$obj->cube_surface_area(15);
	$obj->cube_surface_area(4);
	$obj->cube_surface_area(4.7);
}
main();

Output

 Cube Size [ edge : 7]
 Surface Area : 294

 Cube Size [ edge : 15]
 Surface Area : 1350

 Cube Size [ edge : 4]
 Surface Area : 96

 Cube Size [ edge : 4.7]
 Surface Area : 132.54
// Node Js Program
// Find the surface area of cube
class Cube
{
	//Calculate surface area of cube by given edge
	cube_surface_area(edge)
	{
		// Formula
		// 6 *edge *edge
		var area = 6 *edge *edge;
		//Display result
		process.stdout.write(" Cube Size [ edge : " + edge + "] ");
		process.stdout.write("\n Surface Area : " + area + "\n\n");
	}
}

function main(args)
{
	var obj = new Cube();
	//Test Case
	obj.cube_surface_area(7);
	obj.cube_surface_area(15);
	obj.cube_surface_area(4);
	obj.cube_surface_area(4.7);
}
main();

Output

 Cube Size [ edge : 7]
 Surface Area : 294

 Cube Size [ edge : 15]
 Surface Area : 1350

 Cube Size [ edge : 4]
 Surface Area : 96

 Cube Size [ edge : 4.7]
 Surface Area : 132.54000000000002
#  Python 3 Program
#  Find the surface area of cube
class Cube :
	# Calculate surface area of cube by given edge
	def cube_surface_area(self, edge) :
		#  Formula
		#  6 * edge * edge
		area = 6 * edge * edge
		# Display result
		print(" Cube Size [ edge : ", edge , end = "] ")
		print("\n Surface Area : ", area ,"\n")
	

def main() :
	obj = Cube()
	# Test Case
	obj.cube_surface_area(7)
	obj.cube_surface_area(15)
	obj.cube_surface_area(4)
	obj.cube_surface_area(4.7)


if __name__ == "__main__": main()

Output

 Cube Size [ edge :  7]
 Surface Area :  294

 Cube Size [ edge :  15]
 Surface Area :  1350

 Cube Size [ edge :  4]
 Surface Area :  96

 Cube Size [ edge :  4.7]
 Surface Area :  132.54000000000002
#  Ruby Program
#  Find the surface area of cube
class Cube

	# Calculate surface area of cube by given edge
	def cube_surface_area(edge)
	
		#  Formula
		#  6 * edge * edge
		area = 6 * edge * edge
		# Display result
		print(" Cube Size [ edge  : ", edge ,"] ")
		print("\n Surface Area  : ", area ,"\n\n")
	end
end
def main()

	obj = Cube.new()
	# Test Case
	obj.cube_surface_area(7)
	obj.cube_surface_area(15)
	obj.cube_surface_area(4)
	obj.cube_surface_area(4.7)
end
main()

Output

 Cube Size [ edge  : 7] 
 Surface Area  : 294

 Cube Size [ edge  : 15] 
 Surface Area  : 1350

 Cube Size [ edge  : 4] 
 Surface Area  : 96

 Cube Size [ edge  : 4.7] 
 Surface Area  : 132.54000000000002

// Scala Program
// Find the surface area of cube
class Cube
{
	//Calculate surface area of cube by given edge
	def cube_surface_area(edge: Double): Unit = {
		// Formula
		// 6 * edge * edge
		var area: Double = 6 * edge * edge;
		//Display result
		print(" Cube Size [ edge : " + edge + "] ");
		print("\n Surface Area : " + area + "\n\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: Cube = new Cube();
		//Test Case
		obj.cube_surface_area(7);
		obj.cube_surface_area(15);
		obj.cube_surface_area(4);
		obj.cube_surface_area(4.7);
	}
}

Output

 Cube Size [ edge : 7.0]
 Surface Area : 294.0

 Cube Size [ edge : 15.0]
 Surface Area : 1350.0

 Cube Size [ edge : 4.0]
 Surface Area : 96.0

 Cube Size [ edge : 4.7]
 Surface Area : 132.54000000000002
// Swift Program
// Find the surface area of cube
class Cube
{
	//Calculate surface area of cube by given edge
	func cube_surface_area(_ edge: Double)
	{
		// Formula
		// 6 * edge * edge
		let area: Double = 6 * edge * edge;
		//Display result
		print(" Cube Size [ edge : ", edge , terminator: " ]");
		print("\n Surface Area : ", area ,"\n");
	}
}
func main()
{
	let obj: Cube = Cube();
	//Test Case
	obj.cube_surface_area(7);
	obj.cube_surface_area(15);
	obj.cube_surface_area(4);
	obj.cube_surface_area(4.7);
}
main();

Output

 Cube Size [ edge :  7.0 ]
 Surface Area :  294.0

 Cube Size [ edge :  15.0 ]
 Surface Area :  1350.0

 Cube Size [ edge :  4.0 ]
 Surface Area :  96.0

 Cube Size [ edge :  4.7 ]
 Surface Area :  132.54

Output Explanation

The code calculates the surface area for each test case and displays the results. For a cube with an edge length of 7, the surface area is 294. Similarly, for edge lengths of 15, 4, and 4.7, the surface areas are 1350, 96, and 132.54, respectively.

Time Complexity

The time complexity of this code is constant O(1) because the calculations involve basic arithmetic operations, which take a constant amount of time regardless of the input size. The program performs a fixed number of operations for each test case, making it efficient.

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment