Friday, November 18, 2016

Programming Challenge 4.6 - Newtons Mass

/* Newtons Mass - This program asks the user to enter an object's mass in kg, and
   then calculates and displays its weight. If the object weighs more than 1,000
   newtons, a message is displayed indicating that it is too heavy. If the object
   weighs less than 10 newtons, a message indicating that it is too light is
   displayed.
  
   The following formula is used to calculate the weight in newton of an object:
   * Weight = mass * 9.8 */

#include "Utility.h"

int main()
{
    /* Hold constants for minimum and maximum weight in Newton */
    const double MIN_NEWT_WEIGHT = 10.0;
    const double MAX_NEWT_WEIGHT = 1000.0;

    /* Hold mass and weight */
    double mass,
           newtWeight;

    /* Ask the user for a mass in kg */
    cout << "Enter a mass in kg: ";
    cin >> mass;

    /* Calculate the newtonian weight of the object */
    newtWeight = (mass * 9.8);

    /* Format the output */
    cout << showpoint << fixed << setprecision(2);

    /* Determine if newtonian weight is below minimum or maximum weight, display
       the weight, and indicate whether the object is too heavy or too leight */
    if (newtWeight >= MIN_NEWT_WEIGHT && newtWeight <= MAX_NEWT_WEIGHT)
    {
        cout << "The object with a mass of " << mass << "kg "
            << "has a newtonian weight of " << newtWeight << " newton.\n";
    }
    else if (newtWeight < MIN_NEWT_WEIGHT)
    {
        cout << "The object with a mass of " << mass << "kg "
            << "has a newtonian weight of " << newtWeight << " newton, it is too light.\n";
    }
    else
    {
        cout << "The object with a mass of " << mass << "kg "
            << "has a newtonian weight of " << newtWeight << " newton, it is too heavy.\n";
    }

    pauseSystem();
    return 0;
}

No comments:

Post a Comment