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:

print("Surface Area :", area1)

print("Surface Area :", area2)

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
{
// Formula
// 4πr²
//Display result
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
{
// Formula
// 4πr²
//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
{
// Formula
// 4πr²
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
{
// Formula
// 4πr²
//Display result
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
{
// Formula
// 4πr²
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
{
// Formula
// 4πr²
//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
#  Formula
#  4πr²
# 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

#  Formula
#  4πr²
# Display result
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²
//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
{
// Formula
// 4πr²
//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.

Categories
Relative Post
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "url": "https://kalkicode.com/find-surface-area-sphere", "fetch_time": 1713142207000000000, "content_mime_type": "text/html", "warc_filename": "crawl-data/CC-MAIN-2024-18/segments/1712296816904.18/warc/CC-MAIN-20240414223349-20240415013349-00652.warc.gz", "warc_record_offset": 332713923, "warc_record_length": 9542, "token_count": 2468, "char_count": 8710, "score": 4.0, "int_score": 4, "crawl": "CC-MAIN-2024-18", "snapshot_type": "latest", "language": "en", "language_score": 0.8542366027832031, "file_path": "/sailhome/yjruan/project/latent-lm/data/processed/finemath-4plus/finemath-4plus/train-00054-of-00064.parquet"}