C++ Online Compiler
Example: Restaurant Bill Calculator in C++
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// Restaurant Bill Calculator #include <iostream> // For input/output operations (cin, cout) #include <iomanip> // For output formatting (setprecision, fixed) using namespace std; int main() { // Step 1: Declare variables to store inputs and calculated values double baseMealCost; double taxRate; // as a percentage, e.g., 7 for 7% double tipPercentage; // as a percentage, e.g., 15 for 15% double taxAmount; double tipAmount; double totalBill; // Step 2: Get input from the user for base meal cost cout << "Enter the base cost of the meal: $"; cin >> baseMealCost; // Step 3: Get input for tax rate cout << "Enter the tax rate (e.g., 7 for 7%): "; cin >> taxRate; // Step 4: Get input for tip percentage cout << "Enter the tip percentage (e.g., 15 for 15%): "; cin >> tipPercentage; // Step 5: Convert percentages to decimal for calculation // Example: 7% becomes 0.07, 15% becomes 0.15 double decimalTaxRate = taxRate / 100.0; double decimalTipPercentage = tipPercentage / 100.0; // Step 6: Calculate the tax amount taxAmount = baseMealCost * decimalTaxRate; // Step 7: Calculate the tip amount (usually on the meal cost + tax) // This program calculates tip on (base cost + tax). tipAmount = (baseMealCost + taxAmount) * decimalTipPercentage; // Step 8: Calculate the total bill totalBill = baseMealCost + taxAmount + tipAmount; // Step 9: Display the results with two decimal places for currency cout << fixed << setprecision(2); // Set output to fixed-point notation with 2 decimal places cout << "\n--- Bill Details ---" << endl; cout << "Base Meal Cost: $" << baseMealCost << endl; cout << "Tax Rate (" << taxRate << "%): $" << taxAmount << endl; cout << "Tip (" << tipPercentage << "%): $" << tipAmount << endl; cout << "--------------------" << endl; cout << "Total Bill: $" << totalBill << endl; return 0; // Indicate successful execution }
Output
Clear
ADVERTISEMENTS