Grupo
Control de Flujo en C++
Ojetivo
1. Solicite al usuario que introduzca el número máximo hasta el cual debe imprimirse el patrón.
2. Utilice un bucle para imprimir números desde el 1 hasta el número máximo.
3. Asegúrese de que todos los números se impriman en una sola fila, separados por espacios.
4. Muestre el patrón en la pantalla.
Escriba un programa que imprima un patrón de números (1, 2, 3, 4...).
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 - entry point of the program
int main() {
int n; // Variable to store the maximum number
// Prompt the user to enter the maximum number for the pattern
cout << "Enter the maximum number for the pattern: ";
cin >> n; // Read the input number
// Loop to print numbers from 1 to the specified number
for (int i = 1; i <= n; i++) { // Start from 1 and print numbers up to n
cout << i << " "; // Print the current number followed by a space
}
cout << endl; // Move to the next line after printing the pattern
return 0; // Return 0 to indicate successful execution of the program
}
Salida
Enter the maximum number for the pattern: 10
1 2 3 4 5 6 7 8 9 10
//Or for another input:
Enter the maximum number for the pattern: 5
1 2 3 4 5
Comparte este ejercicio C++