C++ Coding An object\'s mass can be measured in kilograms. The weight is measure
ID: 3873455 • Letter: C
Question
C++ Coding
An object's mass can be measured in kilograms. The weight is measured in newtons. So an object of a specific mass (in kilograms) would have one weight (in newtons) on the earth and a different weight on the moon. Your program will read in a weight and convert it to newtons for the Earth, the Moon, and Mars So, on the Earth we can convert kilograms to newtons with the following expression: weight mass 9.81 where 9.81 is the acceleration due to gravity on earth (in meters per second squared or m/s 2). On the Moon this formula would be: weight mass 1.62 where 1.62 is the acceleration due to gravity on the moon (m/sA2) Finally, for Mars it would be: weight mass 3.77 Again, all of the above assume the mass is in kilograms (kg) and the weight is in newtons (N). You need to write a program that reads in the mass of an object (in kg) and output the weight (in N) on the Earth, on Mars and on the Moon. If the weight on the earth is greater than or equal to 1000 newtons output a message saying the object is heavy, if it is less than 10 newtons say it is light The mass and weight should use fixed notation with a precision of 3 digits to the right of the decimal point. The 1st column has a width of 8 characters and the 2nd column has a width of 12 characters. The 1st column is left justified and the 2nd column is right justified Use double variables and constants in your calculations. The acceleration values must be defined as const double values. These are the 9.81, 1.62 and 3.77 values from above. If the mass is less than O output a message saying the mass must be greater than 0 See the sample output below for the exact syntax of the messages. For example, assume the input is: 101.9367991Explanation / Answer
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double mass,weightEarth,weightMoon,weightMars;
//constants for earth,moon and mars
const double earth = 9.81;
const double moon = 1.62;
const double mars = 3.77;
cout<<fixed<<setprecision(3); //using iomanip header set precision and fixed format
cout<<"Input the mass : ";
cin>>mass;
cout<<" The mass is "<<mass <<" kg";
if(mass <=0)
cout<<" The mass must be greater than 0";
else
{
weightEarth = mass * earth;
weightMoon = mass * moon;
weightMars = mass * mars;
//formatting using setw ,left and right
cout<<" "<<left<<setw(8)<<"Location"<<right<<setw(12)<<"Weight (N)";
cout<<" "<<left<<setw(8)<<"Earth"<<right<<setw(12)<<weightEarth;
cout<<" "<<left<<setw(8)<<"Mars"<<right<<setw(12)<<weightMars;
cout<<" "<<left<<setw(8)<<"Moon"<<right<<setw(12)<<weightMoon;
if(weightEarth < 10)
cout<<" The mass is light";
else if(weightEarth >=1000)
cout<<" The mass is heavy";
}
return 0;
}
Output:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.