Objectivo
Escriba un programa en C# para pedir al usuario un número y muestre ir cuatro veces seguidas, separadas con espacios en blanco y, a continuación, cuatro veces en la fila siguiente, sin separación. Debe hacerlo dos veces: primero usando Console.Write y luego usando {0}.
Ejemplo Ejercicio C#
Mostrar Código C#
using System; // Importing the System namespace to use Console functionalities
// Main class of the program
class Program
{
// Main method where the program execution begins
static void Main()
{
// Declaring a variable to store the number entered by the user
int number;
// Asking the user to enter a number and reading the input
Console.Write("Enter a number: ");
number = Convert.ToInt32(Console.ReadLine());
// Using Console.Write to display the number four times in a row, separated by blank spaces
Console.WriteLine(); // Adding a newline before the first output
Console.Write(number + " " + number + " " + number + " " + number); // Printing the number with spaces
Console.WriteLine(); // Adding a newline after the first output
// Using Console.Write to display the number four times in the next row, with no separation
Console.Write(number.ToString() + number.ToString() + number.ToString() + number.ToString()); // Printing the number without spaces
Console.WriteLine(); // Adding a newline after the second output
// Using {0} formatting to display the number four times in a row, separated by blank spaces
Console.WriteLine("{0} {0} {0} {0}", number); // Printing the number with spaces
// Using {0} formatting to display the number four times in the next row, with no separation
Console.WriteLine("{0}{0}{0}{0}", number); // Printing the number without spaces
}
}
Salida
Enter a number: 7
7 7 7 7
77777777
7 7 7 7
77777777
Código de Ejemplo Copiado!
Comparte este Ejercicio C# Sharp