C++ Program to calculate the volume and area of Sphere

The Sphere is a ball-like 3D object whose all sides are curved surfaces. On a sphere, no plane surface is there. The volume of a sphere is actually how much space it is taking in the space. In this article, we shall cover the techniques to calculate the volume and the surface area of a sphere in C++.

A sphere has one basic parameter. The radius ?. We will see the techniques to calculate the volume and area one by one.

The volume of a Sphere

To calculate the volume of a sphere, we need one input, the radius ?. The formula for the volume is like below ?

$$Volume\:=\:\frac{4}{3}\pi\:r^3$$


Now let us see the algorithm and implementation for the same.

Algorithm

  • Read the radius r as input.
  • Volume := $\frac{4}{3}\pi\:r^3$.
  • Return Volume.

Example

<div class="execute"></div><div class="code-mirror  language-cpp" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><iostream></span></span>
<span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span>

<span class="token keyword">float</span> <span class="token function">solve</span><span class="token punctuation">(</span> <span class="token keyword">float</span> r <span class="token punctuation">)</span> <span class="token punctuation">{</span>
   <span class="token keyword">float</span> volume<span class="token punctuation">;</span>
   volume <span class="token operator">=</span> <span class="token punctuation">(</span> <span class="token number">4</span> <span class="token operator">/</span> <span class="token number">3</span> <span class="token punctuation">)</span> <span class="token operator">*</span> <span class="token number">3.14159</span> <span class="token operator">*</span> <span class="token punctuation">(</span> r <span class="token operator">*</span> r <span class="token operator">*</span> r<span class="token punctuation">)</span><span class="token punctuation">;</span>
   <span class="token keyword">return</span> volume<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)<font color="#000000"> </font></span><span class="token punctuation">{</span>
   cout <span class="token operator"><<</span> <span class="token string">"Volume of a sphere with radius r = 2.5 cm, is "</span> <span class="token operator"><<</span> <span class="token function">solve</span><span class="token punctuation">(</span> <span class="token number">2.5</span> <span class="token punctuation">)</span> <span class="token operator"><<</span> <span class="token string">" cm^3"</span> <span class="token operator"><<</span> endl<span class="token punctuation">;</span>
   cout <span class="token operator"><<</span> <span class="token string">"Volume of a sphere with radius r = 5in, is "</span> <span class="token operator"><<</span> <span class="token function">solve</span><span class="token punctuation">(</span> <span class="token number">5</span> <span class="token punctuation">)</span> <span class="token operator"><<</span> <span class="token string">" in^3"</span> <span class="token operator"><<</span> endl<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

Output

Volume of a sphere with radius r = 2.5 cm, is 49.0873 cm^3
Volume of a sphere with radius r = 5in, is 392.699 in^3

Surface Area of a Sphere

The sphere has only the curved surface, no other plane surface is there. From the radius, we can calculate the surface area by following the simple formula as shown below.

$$Surface\:Area\:=\:4\pi\:r^2$$

The following algorithm is to calculate the surface area for a given Sphere ?

Algorithm

  • Read radius r as input.
  • area := $4\pi\:r^2$.
  • Return area.

Example

<div class="execute"></div><div class="code-mirror  language-cpp" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><iostream></span></span>
<span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span>

<span class="token keyword">float</span> <span class="token function">solve</span><span class="token punctuation">(</span> <span class="token keyword">float</span> r <span class="token punctuation">)</span> <span class="token punctuation">{</span>
   <span class="token keyword">float</span> volume<span class="token punctuation">;</span>
   volume <span class="token operator">=</span> <span class="token number">4</span> <span class="token operator">*</span> <span class="token number">3.14159</span> <span class="token operator">*</span> <span class="token punctuation">(</span> r <span class="token operator">*</span> r <span class="token punctuation">)</span><span class="token punctuation">;</span>
   <span class="token keyword">return</span> volume<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)<font color="#000000"> </font></span><span class="token punctuation">{</span>
   cout <span class="token operator"><<</span> <span class="token string">"Surface Area of a sphere with radius r = 2.5 cm, is "</span> <span class="token operator"><<</span> <span class="token function">solve</span><span class="token punctuation">(</span> <span class="token number">2.5</span> <span class="token punctuation">)</span> <span class="token operator"><<</span> <span class="token string">" cm^3"</span> <span class="token operator"><<</span> endl<span class="token punctuation">;</span>
   cout <span class="token operator"><<</span> <span class="token string">"Surface Area of a sphere with radius r = 5in, is "</span> <span class="token operator"><<</span> <span class="token function">solve</span><span class="token punctuation">(</span> <span class="token number">5</span> <span class="token punctuation">)</span> <span class="token operator"><<</span> <span class="token string">" in^3"</span> <span class="token operator"><<</span> endl<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

Output

Surface Area of a sphere with radius r = 2.5 cm, is 78.5397 cm^3
Surface Area of a sphere with radius r = 5in, is 314.159 in^3

Conclusion

The sphere is a ball-like structure in the 3D world. Like other 3D objects, we can calculate the volume and surface area of a sphere. For a sphere, the diameter or the radius is sufficient to express a sphere. From the diameter or radius, we are calculating the area of the curved surface and also the volume of the sphere. The formula for these two is very simple. In the given implementations, the same formula is used in a straightforward manner.

Updated on: 2022-10-17T12:46:21+05:30

10K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements