//Tom & Jerry's Lawn Service //Student name: Tarun Balaji //Defining the namespace and library #include <iostream> #include <iomanip> using namespace std; // starting code int main() { double mowing_price = 34.95; //fixed mowing price is set as double double fertz_price = 39.95; //fixed fertilizing price is set as double double tree_price = 54.95; //fixed tree price is set as double int LawnArea; //set lawn area to be an integer int fertz_app; //set number int tree_number;//set number of trees to be an integer double mowing_cost; double fertz_cost; double tree_cost; double Total_cost;//set cost values to be doubles cout << "Welcome to Tom & Jerry's Lawn Service!"; //accessory cout << "Enter the area of your lawn (in sq. yards): "; cin >> LawnArea; //storing lawn area from input //calculate mowing cost. use ceil to round up to the nearest whole number mowing_cost = ceil((LawnArea - 1)/4000.0)*mowing_price; cout << "Enter the number of fertilizing applications: "; cin >> fertz_app; //storing no. of fertilizing application from input //calculate cost=number*rate fertz_cost = fertz_app * fertz_price; cout << "Enter the number of trees you want to plant: "; cin >> tree_number; //storing no. of trees from input //calculate cost=number*rate tree_cost = tree_number * tree_price; //calculate total cost through addition and store as Total_cost Total_cost = mowing_cost + fertz_cost + tree_cost; cout << fixed << setprecision(2); //set the output format of the following numbers to two decimal places //display all values using cout cout << "Cost of mowing: $" << mowing_cost << endl; cout << "Cost of fertilizing applications: $" << fertz_cost << endl; cout << "Cost of planting trees: $" << tree_cost << endl; cout << "Total cost: $" << Total_cost << endl; //code returns to starting point return 0; }
Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++
and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}