Posted on by Kalkicode
Code Geometric

Find the surface area of sphere

In geometry, a sphere is a three-dimensional object that is perfectly round, with all points on its surface equidistant from its center. The surface area of a sphere refers to the total area covered by its outer surface.

Problem Statement

Given the radius of a sphere, the task is to calculate its surface area. The formula for finding the surface area of a sphere in terms of its radius 'r' is:

Surface Area = 4 * π * r^2

Example Scenario

Imagine you are an astrophysicist studying planets and celestial bodies. You need to determine the surface area of a planet to understand its heat exchange with space. By calculating the surface area of the planet (approximated as a sphere) using its radius, you can better comprehend its thermal characteristics.

Idea to Solve the Problem

To solve this problem, we can follow these steps:

  1. Accept the radius of the sphere as input.
  2. Use the formula to calculate the surface area of the sphere.
  3. Display the calculated surface area.

Pseudocode

function sphere_area(radius):
    area = 4 * π * radius^2
    return area

main:
    radius1 = 3
    radius2 = 7
    radius3 = 5.8
    
    area1 = sphere_area(radius1)
    area2 = sphere_area(radius2)
    area3 = sphere_area(radius3)
    
    print("Sphere Surface [ radius :", radius1, "]")
    print("Surface Area :", area1)
    
    print("Sphere Surface [ radius :", radius2, "]")
    print("Surface Area :", area2)
    
    print("Sphere Surface [ radius :", radius3, "]")
    print("Surface Area :", area3)

Algorithm Explanation

  1. Define a function sphere_area that takes the radius as input.
  2. Inside the function, use the provided formula to calculate the surface area of the sphere.
  3. In the main function, set three test cases with different radii: 3, 7, and 5.8.
  4. Calculate the surface areas for each test case by calling the sphere_area function.
  5. Display the calculated surface areas along with their respective radii.

Code Solution

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

#include <math.h>
 //Calculate surface area of sphere by given radius
void sphere_area(double radius)
{
	// Formula
	// 4πr²
	double area = 4 * M_PI * (radius * radius);
	//Display result
	printf(" Sphere Surface [ radius : %lf] ", radius);
	printf("\n Surface Area : %lf\n\n", area);
}
int main()
{
	//Test Case
	sphere_area(3);
	sphere_area(7);
	sphere_area(5.8);
	return 0;
}

Output

 Sphere Surface [ radius : 3.000000]
 Surface Area : 113.097336

 Sphere Surface [ radius : 7.000000]
 Surface Area : 615.752160

 Sphere Surface [ radius : 5.800000]
 Surface Area : 422.732707
// Java Program
// Find the surface area of sphere
class Sphere
{
	//Calculate surface area of sphere by given radius
	public void sphere_area(double radius)
	{
		// Formula
		// 4πr²
		double area = 4 * Math.PI * (radius * radius);
		//Display result
		System.out.print(" Sphere Surface [ radius : " + radius + "] ");
		System.out.print("\n Surface Area : " + area + "\n\n");
	}
	public static void main(String[] args)
	{
		Sphere obj = new Sphere();
		//Test Case
		obj.sphere_area(3);
		obj.sphere_area(7);
		obj.sphere_area(5.8);
	}
}

Output

 Sphere Surface [ radius : 3.0]
 Surface Area : 113.09733552923255

 Sphere Surface [ radius : 7.0]
 Surface Area : 615.7521601035994

 Sphere Surface [ radius : 5.8]
 Surface Area : 422.7327074670426
// C++ Program
// Find the surface area of sphere
#include<iostream>
#include <math.h>
using namespace std;
class Sphere
{
	public:
		//Calculate surface area of sphere by given radius
		void sphere_area(double radius)
		{
			// Formula
			// 4πr²
			double area = 4 *M_PI *(radius *radius);
			cout << " Sphere Surface [ radius : " << radius << "] ";
			cout << "\n Surface Area : " << area << "\n\n";
		}
};
int main()
{
	Sphere obj =  Sphere();
	//Test Case
	obj.sphere_area(3);
	obj.sphere_area(7);
	obj.sphere_area(5.8);
	return 0;
}

Output

 Sphere Surface [ radius : 3]
 Surface Area : 113.097

 Sphere Surface [ radius : 7]
 Surface Area : 615.752

 Sphere Surface [ radius : 5.8]
 Surface Area : 422.733
<?php
// Php Program
// Find the surface area of sphere
class Sphere
{
	//Calculate surface area of sphere by given radius
	public 	function sphere_area($radius)
	{
		// Formula
		// 4πr²
		$area = 4 * M_PI *($radius *$radius);
		//Display result
		echo(" Sphere Surface [ radius : ". $radius ."] ");
		echo("\n Surface Area : ". $area ."\n\n");
	}
}

function main()
{
	$obj = new Sphere();
	//Test Case
	$obj->sphere_area(3);
	$obj->sphere_area(7);
	$obj->sphere_area(5.8);
}
main();

Output

 Sphere Surface [ radius : 3]
 Surface Area : 113.09733552923

 Sphere Surface [ radius : 7]
 Surface Area : 615.7521601036

 Sphere Surface [ radius : 5.8]
 Surface Area : 422.73270746704
// C# Program
// Find the surface area of sphere
using System;
class Sphere
{
	//Calculate surface area of sphere by given radius
	public void sphere_area(double radius)
	{
		// Formula
		// 4πr²
		double area = 4 * Math.PI * (radius * radius);
		Console.Write(" Sphere Surface [ radius : " + radius + "] ");
		Console.Write("\n Surface Area : " + area + "\n\n");
	}
	public static void Main(String[] args)
	{
		Sphere obj = new Sphere();
		//Test Case
		obj.sphere_area(3);
		obj.sphere_area(7);
		obj.sphere_area(5.8);
	}
}

Output

 Sphere Surface [ radius : 3]
 Surface Area : 113.097335529233

 Sphere Surface [ radius : 7]
 Surface Area : 615.752160103599

 Sphere Surface [ radius : 5.8]
 Surface Area : 422.732707467043
// Node Js Program
// Find the surface area of sphere
class Sphere
{
	//Calculate surface area of sphere by given radius
	sphere_area(radius)
	{
		// Formula
		// 4πr²
		var area = 4 * Math.PI * (radius *radius);
		//Display result
		process.stdout.write(" Sphere Surface [ radius : " + radius + "] ");
		process.stdout.write("\n Surface Area : " + area + "\n\n");
	}
}

function main(args)
{
	var obj = new Sphere();
	//Test Case
	obj.sphere_area(3);
	obj.sphere_area(7);
	obj.sphere_area(5.8);
}
main();

Output

 Sphere Surface [ radius : 3]
 Surface Area : 113.09733552923255

 Sphere Surface [ radius : 7]
 Surface Area : 615.7521601035994

 Sphere Surface [ radius : 5.8]
 Surface Area : 422.7327074670426
#  Python 3 Program
#  Find the surface area of sphere
import math
class Sphere :
	# Calculate surface area of sphere by given radius
	def sphere_area(self, radius) :
		#  Formula
		#  4πr²
		area = 4 * math.pi * (radius * radius)
		# Display result
		print(" Sphere Surface [ radius : ", radius ,"] ", end = "")
		print("\n Surface Area : ", area ,"\n\n", end = "")
	

def main() :
	obj = Sphere()
	# Test Case
	obj.sphere_area(3)
	obj.sphere_area(7)
	obj.sphere_area(5.8)


if __name__ == "__main__": main()

Output

 Sphere Surface [ radius :  3 ]
 Surface Area :  113.09733552923255

 Sphere Surface [ radius :  7 ]
 Surface Area :  615.7521601035994

 Sphere Surface [ radius :  5.8 ]
 Surface Area :  422.7327074670426
#  Ruby Program
#  Find the surface area of sphere
class Sphere

	# Calculate surface area of sphere by given radius
	def sphere_area(radius)
	
		#  Formula
		#  4πr²
		area = 4 * Math::PI * (radius * radius)
		# Display result
		print(" Sphere Surface [ radius  : ", radius ,"] ")
		print("\n Surface Area  : ", area ,"\n\n")
	end
end
def main()

	obj = Sphere.new()
	# Test Case
	obj.sphere_area(3)
	obj.sphere_area(7)
	obj.sphere_area(5.8)
end
main()

Output

 Sphere Surface [ radius  : 3] 
 Surface Area  : 113.09733552923255

 Sphere Surface [ radius  : 7] 
 Surface Area  : 615.7521601035994

 Sphere Surface [ radius  : 5.8] 
 Surface Area  : 422.7327074670426

// Scala Program
// Find the surface area of sphere
class Sphere
{
	//Calculate surface area of sphere by given radius
	def sphere_area(radius: Double): Unit = {
		// Formula
		// 4πr²
		var area: Double = 4 * Math.PI * (radius * radius);
		//Display result
		print(" Sphere Surface [ radius : " + radius + "] ");
		print("\n Surface Area : " + area + "\n\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: Sphere = new Sphere();
		//Test Case
		obj.sphere_area(3);
		obj.sphere_area(7);
		obj.sphere_area(5.8);
	}
}

Output

 Sphere Surface [ radius : 3.0]
 Surface Area : 113.09733552923255

 Sphere Surface [ radius : 7.0]
 Surface Area : 615.7521601035994

 Sphere Surface [ radius : 5.8]
 Surface Area : 422.7327074670426
// Swift Program
// Find the surface area of sphere

class Sphere
{
	//Calculate surface area of sphere by given radius
	func sphere_area(_ radius: Double)
	{
		// Formula
		// 4πr²
		let area: Double = 4 * Double.pi * (radius * radius);
		//Display result
		print(" Sphere Surface [ radius : ", radius ,"] ", terminator: "");
		print("\n Surface Area : ", area ,"\n\n", terminator: "");
	}
}
func main()
{
	let obj: Sphere = Sphere();
	//Test Case
	obj.sphere_area(3);
	obj.sphere_area(7);
	obj.sphere_area(5.8);
}
main();

Output

 Sphere Surface [ radius :  3.0 ]
 Surface Area :  113.097335529233

 Sphere Surface [ radius :  7.0 ]
 Surface Area :  615.752160103599

 Sphere Surface [ radius :  5.8 ]
 Surface Area :  422.732707467043

Output Explanation

The code calculates the surface area for each test case and displays the results. For a sphere with a radius of 3, the surface area is approximately 113.097336. Similarly, for radii of 7 and 5.8, the surface areas are approximately 615.752160 and 422.732707, 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