Chapter 4 – #6: Mass and Weight – Tony Gaddis – Starting Out With C++

>_ Problem

Scientists measure an object’s mass in kilograms and its weight in newtons. If you know the amount of mass that an object has, you can calculate its weight, in newtons, with the following formula:

Weight = mass x 9.8

Write a program that asks the user to enter an object’s mass, and then calculates and displays its weight. If the object weighs more than 1,000 newtons, display a message indicating that it is too heavy. If the object weighs less than 10 newtons, display a message indicating that the object is too light.

>_ Solution

#include <iostream>
using namespace std;

int main()
{
    // Variables
    float weight, 
          mass;

    // Ask the user for object's mass
    cout << endl;
    cout << "What is the object's mass: ";
    cin >> mass;

    // Calculate object's weight
    weight = mass * 9.8;

    // Display weight.
    cout << "\nObject's weight = ";
    cout << weight << endl << endl;

    // Decision Statement
    if (weight >= 1000)
        cout << "Too heavy.";
    else if (weight <= 10)
        cout << "Too light.";
    else
        cout << "Neither heavy or light.";

    cout << endl << endl;

    // Terminate program
    return 0;
}

>_Get The Book

Tony Gaddis’ book “Starting Out with C++ from Control Structures to Objects”: 8th and 9th editions.

>_ Video Walk Through

This video includes a step by step process of how I solve programming challenge #6.

>_That’s it!

Thank you for taking interest in what I do! I hope it was helpful for you as much as it helped me along my journey in learning to code!

>_ Also Check Out:

Tag: Starting Out With C++ | Chapter 4 | Programming Challenge Solutions

Leave a Reply