1
// #include <iostream>
   #include <cmath>
   using namespace std;
// variable
float r, v;

// input
cout << "To find the volume of a sphere enter the radius: ";
cin >> r;

// formula
v = (4 / 3)*3.14*(pow(r, 3));


// output

cout << "The volume of the sphere is: " << v << endl;



system("pause");
return 0;

ok here is my code, I am trying to find the volume of a sphere but when I divide the 4/3 divides into 1 not 1.3333333 and its not giving me the correct answer can someone please explain to me, try to not give me the answer just an explanation so I can figure it out on my own

user8190367
  • 13
  • 1
  • 5
  • With integer math `4 / 3` == 1. Change to `4.0 / 3` – drescherjm Jan 29 '18 at 23:23
  • try: ((float)4 / 3)*3.14*(pow(r, 3)); 4 is one integer, 3 is one integer, so (4/3) will return one integer. – Sphinx Jan 29 '18 at 23:23
  • @drescherjm thank you it worked, but can you explain it to me why 4.0 worked and not 4 – user8190367 Jan 29 '18 at 23:26
  • I think the duplicate explains that. Remember that 4 and 3 are integers. The division is done as an integer (not double or float) so `4 / 3 is 1` changing one of these to a double causes the math to be done as a double not integer math. – drescherjm Jan 29 '18 at 23:33

0 Answers0