Hello, I made the following code based on the following physics problem:
"A sphere of metal has a radius of 6.7 cm and a density of 9.27 g/cm^3. What's the mass of the sphere?".
The program runs error-free, except that it gives me a result (mass) of 8758.99 grams. The result using my TI 89 calculator is 11678.7 grams. I believe the TI 89 must be correct. What could be the problem?
Thanks a bunch!

#include "stdafx.h"
#include <cmath>
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	double radius, density, mass, volume, pi;
	
	cout<<"Enter radius: \n";
	cin>>radius;
	cout<<"Enter density: \n";
	cin>>density;
	
         pi=3.14159265;
	volume=(4/3)*pi*pow(radius,3);
	mass=density*volume;
	
         cout<<"The mass of the sphere is: "<<mass<<" grams.\n";
 	return 0;
}
volume=(4/3)*pi*pow(radius,3);

Integer division. 4 / 3 is 1, not 1.333. Try replacing with floating point to avoid this problem.

volume=(4.0/3.0)*pi*pow(radius,3);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.