How to find Volume of a Sphere using C++ Code

Eiman.pk has uploaded detailed article about how to code and calculate the volume of a sphere having the formula

V = 4/3*pi*r3

Code Elements:

To draw these shapes we are going to use following C++ elements and escape sequences:

  • #include <iostream> – We are using this to specify Library or preprocess directives.
  • using namespace std; – We are using this to specify the part of library further.
  • main () – We are not using int main because our code don’t contains integers.
  • const float – For fixing point values like pi
  • cout<<” “; – For directly printing our output shapes.\
  • <<endl; – We will add this element with cout to end our line

Which Compiler is best?

  • For Laptop or PC Devices use Visual Studio Code or CFree
  • For Mobile Devices you can use online Compilers such as w3shools. The link is given below:

W3Schools Try it Yourself Editor

Code for Volume of Sphere:

1- First, we will specify the library.

#include <iostream>
using namespace std;
int main ()
{
}

2- Then we will specify variables.

#include <iostream>
using namespace std;
int main ()
{
const float pi = 3.14;
float r = 9.6
}

3- Now we will process the formula.

#include <iostream>
using namespace std;
int main ()
{
const float pi = 3.14;
float r = 9.6
float V=(4*3.14*r*r*r)/3;
}

4- Now we will cout the volume of sphere.

#include <iostream>
using namespace std;
int main ()
{
const float pi=3.14;
float r=9.6;
float r_cube = r*r*r;
float V=(4)*((pi*r_cube)/3);
cout<<"Volume of the sphere is "; 
cout<<V; cout<<" cubic cm volume";
}

Leave a Reply

Featured Posts

2nd year Physics Chapter 20 notes

FSc 2nd year Physics Chapter 20 Notes – LQs, SQs, Numericals, MCQs

READ

FSc 2nd year Computer Science Chapter 4 Notes

READ

FSc 2nd year Computer Science Chapter 11 Notes

READ

FSc 2nd year Math Chapter 6 Notes – Conic section

READ

Electrostatics: Definition | Charge | Point Charges | Electric Force

READ
car insurance

Car Insurance: Ultimate Guide | Importance | Types | How it Works?

READ