Find the surface area of sphere
Here given code implementation process.
//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
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