fork download
  1. // Chapter 4 - Programming Challenge #6
  2. // Mass and Weight
  3. // This program asks the user to enter an object's mass
  4. // in kilograms. It calculates the object's weight in
  5. // newtons and determines whether the object is too
  6. // heavy, too light, or within the normal range.
  7.  
  8. #include <iostream>
  9. using namespace std;
  10.  
  11. int main()
  12. {
  13. // Variables to store the object's mass and weight
  14. double mass, weight;
  15.  
  16. // Get the object's mass from the user
  17. cout << "Enter the object's mass in kilograms: ";
  18. cin >> mass;
  19.  
  20. // Calculate the object's weight in newtons
  21. weight = mass * 9.8;
  22.  
  23. // Display the calculated weight
  24. cout << "The object's weight is " << weight
  25. << " newtons." << endl;
  26.  
  27. // Determine whether the object is too heavy,
  28. // too light, or within the normal range
  29. if (weight > 1000)
  30. {
  31. cout << "The object is too heavy." << endl;
  32. }
  33. else if (weight < 10)
  34. {
  35. cout << "The object is too light." << endl;
  36. }
  37. else
  38. {
  39. cout << "The object's weight is within the normal range." << endl;
  40. }
  41.  
  42. return 0;
  43. }
Success #stdin #stdout 0s 5328KB
stdin
Standard input is empty
stdout
Enter the object's mass in kilograms: The object's weight is 6.81421e-309 newtons.
The object is too light.