Program to Compute Volume and Surface Area of a Shpere
- Write a program to Compute Volume and Surface Area of a Shpere in C
- Write a program to Compute Volume and Surface Area of a Shpere in C++
- Write a program to Compute Volume and Surface Area of a Shpere in Python
- Write a program to Compute Volume and Surface Area of a Shpere in PHP
- Write a program to Compute Volume and Surface Area of a Shpere in Java
- Write a program to Compute Volume and Surface Area of a Shpere in Java Script
- Write a program to Compute Volume and Surface Area of a Shpere in C#
Explanation:
A sphere is a perfectly round, three-dimensional geometric object. The key characteristics of a sphere include that all points on its surface are equidistant from the center.
Formulas:
Volume (V): 4/3 x PI x r3
Surface Area (SA): Surface Area = 4 x PI x r2
r = Radius of the base
Program to Compute Volume and Surface Area of a Sphere
-
C
-
C++
-
Python
-
PHP
-
JAVA
-
Java Script
-
C#
#include <stdio.h>
#include <math.h>
int main() {
float radius, volume, surface_area;
// Input the radius of the sphere
printf("Enter the radius of the sphere: ");
scanf("%f", &radius);
// Calculate Volume and Surface Area
volume = (4.0 / 3.0) * M_PI * pow(radius, 3);
surface_area = 4 * M_PI * pow(radius, 2);
// Output the results
printf("Volume of the sphere: %.2f\n", volume);
printf("Surface area of the sphere: %.2f\n", surface_area);
return 0;
}
#include <iostream>
#include <cmath>
using namespace std;
int main() {
float radius, volume, surface_area;
// Input the radius of the sphere
cout << "Enter the radius of the sphere: ";
cin >> radius;
// Calculate Volume and Surface Area
volume = (4.0 / 3.0) * M_PI * pow(radius, 3);
surface_area = 4 * M_PI * pow(radius, 2);
// Output the results
cout << "Volume of the sphere: " << volume << endl;
cout << "Surface area of the sphere: " << surface_area << endl;
return 0;
}
<div class="contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950"><div class="overflow-y-auto p-4" dir="ltr"><code class="!whitespace-pre hljs language-python">import math
# Input radius of the sphere
radius = float(input("Enter the radius of the sphere: "))
# Calculate Volume and Surface Area
volume = (4 / 3) * math.pi * radius**3
surface_area = 4 * math.pi * radius**2
# Output results
print(f"Volume of the sphere: {volume:.2f}")
print(f"Surface area of the sphere: {surface_area:.2f}")
<?php
$radius = (float)readline("Enter the radius of the sphere: ");
$volume = (4/3) * pi() * pow($radius, 3);
$surface_area = 4 * pi() * pow($radius, 2);
echo "Volume of the sphere: " . number_format($volume, 2) . "\n";
echo "Surface area of the sphere: " . number_format($surface_area, 2) . "\n";
?>
>
import java.util.Scanner;
public class Sphere {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input the radius of the sphere
System.out.print("Enter the radius of the sphere: ");
double radius = scanner.nextDouble();
// Calculate Volume and Surface Area
double volume = (4.0 / 3.0) * Math.PI * Math.pow(radius, 3);
double surfaceArea = 4 * Math.PI * Math.pow(radius, 2);
// Output the results
System.out.printf("Volume of the sphere: %.2f\n", volume);
System.out.printf("Surface area of the sphere: %.2f\n", surfaceArea);
scanner.close();
}
}
// Input the radius of the sphere
const radius = parseFloat(prompt("Enter the radius of the sphere:"));
// Calculate Volume and Surface Area
const volume = (4 / 3) * Math.PI * Math.pow(radius, 3);
const surfaceArea = 4 * Math.PI * Math.pow(radius, 2);
// Output the results
console.log(`Volume of the sphere: ${volume.toFixed(2)}`);
console.log(`Surface area of the sphere: ${surfaceArea.toFixed(2)}`);
using System;
class Program {
static void Main() {
// Input the radius of the sphere
Console.Write("Enter the radius of the sphere: ");
double radius = Convert.ToDouble(Console.ReadLine());
// Calculate Volume and Surface Area
double volume = (4.0 / 3.0) * Math.PI * Math.Pow(radius, 3);
double surfaceArea = 4 * Math.PI * Math.Pow(radius, 2);
// Output the results
Console.WriteLine($"Volume of the sphere: {volume:F2}");
Console.WriteLine($"Surface area of the sphere: {surfaceArea:F2}");
}
}