Define a single dimension array of strings to hold the name of City. Accept some values from the user and store them in the array. Use foreach loop to print all the data of the array.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace C_SHARP
{
class Program
{
static void Main(string[] args)
{
int numberCities;
Console.Write("Enter the number of city: ");
numberCities = int.Parse(Console.ReadLine());
//Define a single dimension array of strings to hold the name of City.
string[] cites = new string[numberCities];
//Accept some values from the user and store them in the array.
for (int i = 0; i < numberCities; i++)
{
Console.Write("Enter a city {0}: ", (i + 1));
cites[i] = Console.ReadLine();
}
//Use foreach loop to print all the data of the array.
Console.WriteLine("\nAll cities are:");
foreach (var c in cites)
{
Console.WriteLine(c);
}
Console.ReadKey();
}
}
}
Comments
Leave a comment