In: Computer Science
Write a program that accepts as input the mass, in grams, and density, in grams per cubic centimeters, and outputs the volume of the object using the formula: volume = mass / density.
Format your output to two decimal places.
** Add Comments
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
//variable declarations and initializations
double mass, density, volume;
mass = 0, density = 0, volume = 0;
//setting the output to have only two decimal places, even if the answer is an integer (10 would output as 10.00)
cout << fixed << showpoint;
cout << setprecision(2);
//user prompts and user inputs
cout << "Please enter the mass of an object in grams: ";
cin >> mass;
cout << "Please enter the density of an object in cubic centimeters: ";
cin >> density;
volume = mass / density;
cout << '\n';
cout << "The volume of the object is: " << volume << " cubic centimeters. \n";
cout << '\n';
//system("pause");
return 0;
}
//end Main()
stdin
10 20
stdout
Please enter the mass of an object in grams: Please enter the density of an object in cubic centimeters: The volume of the object is: ----- cubic centimeters.