Grupo
Control de Flujo en C++
Ojetivo
1. Usa un bucle para iterar por las filas del triángulo.
2. Para cada fila, usa un bucle anidado para imprimir el número adecuado de asteriscos.
3. Prueba el programa con diferentes números de filas para asegurarte de que funciona correctamente.
Escribe un programa que imprima un triángulo de asteriscos.
Ejemplo de Código C++
Mostrar Código C++
#include <iostream> // Include the iostream library for input and output
using namespace std; // Use the standard namespace
// Main function - the entry point of the program
int main() {
int rows; // Variable to store the number of rows for the triangle
// Ask the user to enter the number of rows for the triangle
cout << "Enter the number of rows for the triangle: ";
cin >> rows; // Read the user input and store it in the variable rows
// Outer loop to handle the number of rows
for (int i = 1; i <= rows; ++i) {
// Inner loop to print the asterisks for each row
for (int j = 1; j <= i; ++j) {
cout << "*"; // Print an asterisk for the current position
}
cout << endl; // Move to the next line after printing all asterisks for this row
}
return 0; // Return 0 to indicate that the program executed successfully
}
Salida
Enter the number of rows for the triangle: 5
*
**
***
****
*****
Comparte este ejercicio C++