I have to make a program, which calculates (based on the radius) the length of the circle, area of the circle, volume of the sphere. The problem is that when the values are going to be returned, only the length and the area are correct. The volume gives me a value that does not correspond to the radius that I enter and it is because I do not know how to give priority to the operations. Could you explain me?
#include <iostream>
#include <math.h>
using namespace std;
/*
*
* Programa C++ que lee el radio y calcula:
* - longitud de la circunferencia
* - área del circulo
* - volumen de la esfera
* correspondiente al radio introducido
*/
float circunferencia (float radio){
float resultado;
const float pi=3.14;
resultado = (2 * radio * pi);
cout<<"La circunferencia es: "<<resultado<<endl;
}
float area (float radio){
float resultado;
const float pi=3.14;
resultado = (pi * pow(radio,2));
cout<<"El area es: "<<resultado<<endl;
}
float volumen (float radio){
float resultado;
float pi = 3.14;
resultado = (4/3) * pi * pow(radio,3);
cout<<"El volumen es: "<<resultado<<endl;
}
int main () {
float radio;
cout<<"Ingrese el radio: "<<endl;
cin>>radio;
circunferencia(radio);
area(radio);
volumen(radio);
return 0;
}
The value it gives when I put the radius (10) is = 3140. But the correct value it should give is = 4186
It sounds like those methods shouldn't be floats because they don't return anything, and the numbers you use should be floats and not integers so the operations don't make assumptions.
The operation (4/3) gives an integer result (a 1), and therefore it multiplies by 1 and not by 1.33. Use for example 4.0/3
Change the
resultado = (4/3) * pi * pow(radio,3);
To:
resultado = (4.0/3) * pi * pow(radio,3);
Another issue, the definition of IP I advise you to be global. Imagine that later you want to add a decimal, you should look for all the definitions of the code.