Ejercicio
Estructuras Repetitivas
Objectivo
Cree un programa en C# para pedir al usuario dos números y muestre los números entre ellos (ambos incluidos), tres veces: usando "for", usando "while" y usando "do while"
Introduzca el primer número: 6
Introduzca el último número 12
6 7 8 9 10 11 12
6 7 8 9 10 11 12
6 7 8 9 10 11 12
Ejemplo Ejercicio C#
Mostrar Código C#
using System; // Importing the System namespace to use Console functionalities
class Program
{
// Main method where the program execution begins
static void Main()
{
// Prompt the user to enter the first number
Console.Write("Enter the first number: ");
int firstNumber = int.Parse(Console.ReadLine()); // Reading the first number entered by the user
// Prompt the user to enter the last number
Console.Write("Enter the last number: ");
int lastNumber = int.Parse(Console.ReadLine()); // Reading the last number entered by the user
// Using a for loop to display numbers from firstNumber to lastNumber
Console.WriteLine("Using for loop:");
for (int i = firstNumber; i <= lastNumber; i++) // Loop runs from firstNumber to lastNumber
{
Console.Write(i + " "); // Display the current number
}
Console.WriteLine(); // Move to the next line after printing the numbers
// Using a while loop to display numbers from firstNumber to lastNumber
Console.WriteLine("Using while loop:");
int j = firstNumber; // Initialize the counter for the while loop
while (j <= lastNumber) // Loop runs while j is less than or equal to lastNumber
{
Console.Write(j + " "); // Display the current number
j++; // Increment the counter
}
Console.WriteLine(); // Move to the next line after printing the numbers
// Using a do-while loop to display numbers from firstNumber to lastNumber
Console.WriteLine("Using do-while loop:");
int k = firstNumber; // Initialize the counter for the do-while loop
do
{
Console.Write(k + " "); // Display the current number
k++; // Increment the counter
}
while (k <= lastNumber); // Loop continues while k is less than or equal to lastNumber
Console.WriteLine(); // Move to the next line after printing the numbers
}
}
Salida
Enter the first number: 3
Enter the last number: 6
Using for loop:
3 4 5 6
Using while loop:
3 4 5 6
Using do-while loop:
3 4 5 6
Código de Ejemplo Copiado!
Comparte este Ejercicio C# Sharp
Más Ejercicios de Programacion C# Sharp de Control del Flujo
¡Explora nuestro conjunto de ejercicios de programación C# Sharp! Estos ejercicios, diseñados específicamente para principiantes, te ayudarán a desarrollar una sólida comprensión de los conceptos básicos de C#. Desde variables y tipos de datos hasta estructuras de control y funciones simples, cada ejercicio está diseñado para desafiarte de manera gradual a medida que adquieres confianza en la codificación en C#.
Este ejercicio en C# tiene como objetivo enseñar cómo desarrollar un programa que calcule el número de dígitos en un número entero positivo. El programa utiliza una t...
Este ejercicio en C# tiene como objetivo desarrollar un programa que pida al usuario un símbolo y una anchura, y luego muestre un cuadrado hueco de esa anchura...
Este ejercicio en C# tiene como objetivo desarrollar un programa que pida al usuario dos números enteros y muestre su multiplicación, pero sin utilizar el operador ...
Este ejercicio en C# tiene como objetivo desarrollar un programa que calcule (y muestre) el valor absoluto de un número x. El valor absoluto de un númer...
Este ejercicio en C# tiene como objetivo desarrollar un programa que solicite al usuario un símbolo, un ancho y una altura, y luego muestre un rectán...
Este ejercicio en C# tiene como objetivo desarrollar un programa que permita al usuario ingresar varios números y calcular operaciones estadísticas básicas como la suma...