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
** Print Name and Assignment on screen
** Date
** Submit .cpp file.
Demo
// This program uses a type cast to avoid an integer division.
#include <iostream> // input - output stream
#include <fstream> //working file stream
#include <iomanip> // format output stream OR format file output stream
#include <string>
#include <cmath>
using namespace std;
int main()
{
//------------------------------------------
cout << "pow(2, 2) : " << pow(2, 2) << endl;
//------------------------------------------
//ifstream input; //cin to file
//ofstream output; // cout to file
//------------------------------------------
int books, months;
double booksPerMonth;
// Get user inputs
cout << "How many books do you plan to read? ";
cin >> books;
cout << "How many months will it take you to read them? ";
cin >> months;
// Compute and display books read per month
booksPerMonth = static_cast<double>(books) / months;
cout << "That is " << booksPerMonth << " books per month.\n";
booksPerMonth = (books) / months;
cout << "That is " << booksPerMonth << " books per month.\n";
/*--------------------------------------------------------------*/
int number = 65;
// Display the value of the number variable
cout << "Display the value of the number variable: " << number << endl;
// Use a type cast to display the value of number
// converted to the char data type
cout << static_cast<char>(number) << endl;
cout << static_cast<int>('A') << endl;
/*--------------------------------------------------------------*/
int num1 = 2897, num2 = 5, num3 = 837,
num4 = 34, num5 = 7, num6 = 1623,
num7 = 390, num8 = 3456, num9 = 12;
// Display the first row of numbers
cout << num1 << " " << num2 << " " << num3 << endl;
// Display the second row of numbers
cout << num4 << " " << num5 << " " << num6 << endl;
// Display the third row of numbers
cout << num7 << " " << num8 << " " << num9 << endl << endl;
/*-------------------------FORMAT USING setw( int )-------------------------------------*/
// Display the first row of numbers
cout << setw(6) << num1 << setw(6) << num2 << setw(6) << num3 << endl;
// Display the second row of numbers
cout << setw(6) << num4 << setw(6) << num5 << setw(6) << num6 << endl;
// Display the third row of numbers
cout << setw(6) << num7 << setw(6) << num8 << setw(6) << num9 << endl;
/*--------------------------------------------------------------*/
int intValue = 3928;
double doubleValue = 91.5;
string stringValue = "Jill Q. Jones";
cout << setfill('*');
cout << "(" << setw(5) << intValue << ")" << endl;
cout << "(" << setw(8) << doubleValue << ")" << endl;
cout << "(" << setw(16) << stringValue << ")" << endl;
cout << setfill(' ');
cout << "(" << setw(5) << intValue << ")" << endl;
cout << "(" << setw(8) << doubleValue << ")" << endl;
cout << "(" << setw(16) << stringValue << ")" << endl;
///*--------------------------------------------------------------*/
double x = 7;
cout << x << endl;
cout << showpoint << x << endl;
cout << fixed << x << endl;
cout << setprecision(2) << fixed << x << endl;
/*--------------------------------------------------------------*/
string name;
string city;
cout << "Please enter your name: ";
getline(cin, name);
cout << "Enter the city you live in: ";
getline(cin, city);
cout << "Hello, " << name << endl;
cout << "You live in " << city << endl;
/*--------------------------------------------------------------*/
string firstName , lastName, fullName;
cout << "Please enter your first name: ";
cin >> firstName;
cout << "Please enter your last name: ";
cin >> lastName;
fullName = firstName + " " + lastName;
cout << fullName << endl;
system("pause");
return 0;
}
Here is your C++ code. If you have any query then ask me in comment section. Please give a thumbs up if you find the answer helpful.
#include<iostream> //for cin and cout
#include<iomanip> //for setprecision() and fixed
using namespace std;
int main()
{
//Uncomment below part to show Name and Date
/*
cout<<"Name : Your_Name_Here"<<endl;
cout<<"Date : Date_Here<<endl;
*/
float mass, volume, density; //variables to be used
cout<<"Enter the mass of object in grams : ";
cin>>mass; //reading mass as an user input
cout<<"\nEnter the density of object in grams per cubic centimeters : ";
cin>>density; //reading density as an user input
volume = mass/density; //calculating volume according to the formula
cout<<fixed<<"Volume of the object is : "<<setprecision(2)<<volume<<" cubic centimeters.";
/*
fixed is a Fixed Floating Point Notation.
The value is represented with exactly as many digits in the exponent part as specified
by the precision field (setprecision()).
*/
}
Get Answers For Free
Most questions answered within 1 hours.