Grupo
Introducción a C++
Ojetivo
1. Declare dos variables de punto flotante para almacenar la longitud y el ancho del rectángulo.
2. Solicite al usuario que introduzca la longitud y el ancho.
3. Utilice la fórmula: perímetro = 2 * (longitud + ancho) para calcular el perímetro.
4. Muestre el resultado del perímetro con un mensaje claro.
Calcule el perímetro de un rectángulo.
Ejemplo de Código C++
Mostrar Código C++
#include <iostream> // Include the iostream library for input and output
#include <iomanip> // Include iomanip for formatting output precision
using namespace std; // Use the standard namespace
// Main function - program entry point
int main() {
double length, width, perimeter; // Declare variables for length, width, and perimeter
// Prompt the user to enter the length of the rectangle
cout << "Enter the length of the rectangle: ";
cin >> length; // Read the length from user input
// Prompt the user to enter the width of the rectangle
cout << "Enter the width of the rectangle: ";
cin >> width; // Read the width from user input
// Calculate the perimeter using the formula: 2 * (length + width)
perimeter = 2 * (length + width);
// Set output precision to 2 decimal places and display the result
cout << fixed << setprecision(2);
cout << "The perimeter of the rectangle is: " << perimeter << endl;
return 0; // Return 0 to indicate successful execution
}
Salida
Enter the length of the rectangle: 8
Enter the width of the rectangle: 4
The perimeter of the rectangle is: 24.00
Comparte este ejercicio C++