Group
Introduction to C++
Objective
1. Declare two floating-point variables to hold the amount in euros and the result in dollars.
2. Define a constant variable for the exchange rate (e.g., 1 euro = 1.1 dollars).
3. Prompt the user to enter the amount in euros.
4. Multiply the amount by the exchange rate to get the value in dollars.
5. Display the converted amount with an appropriate message.
Perform a currency conversion (for example, euros to dollars).
Example C++ Exercise
Show C++ Code
#include <iostream> // Include the iostream library to enable input and output operations
#include <iomanip> // Include iomanip for controlling output precision
using namespace std; // Use the standard namespace
// Main function - starting point of the program
int main() {
double euros, dollars; // Declare variables for euros and dollars
const double exchangeRate = 1.1; // Define a constant for the euro to dollar conversion rate
// Prompt the user to enter an amount in euros
cout << "Enter the amount in euros: ";
cin >> euros; // Read the amount in euros from user input
// Convert euros to dollars by multiplying with the exchange rate
dollars = euros * exchangeRate;
// Display the result with two decimal places
cout << fixed << setprecision(2); // Set precision for output
cout << euros << " euros is equal to " << dollars << " US dollars." << endl;
return 0; // Return 0 to indicate successful execution
}
Output
Enter the amount in euros: 50
50.00 euros is equal to 55.00 US dollars.