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 for-each 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 n;
Console.Write("Enter the number of city: ");
n = int.Parse(Console.ReadLine());
//Define a single dimension array of strings to hold the name of City.
string[] cites = new string[n];
//Accept some values from the user and store them in the array.
for (int i = 0; i < n; i++)
{
Console.Write("Enter a new city {0}: ", (i + 1));
cites[i] = Console.ReadLine();
}
//Use foreach loop to print all the data of the array.
Console.WriteLine(Environment.NewLine + "The cities are:");
foreach (var city in cites)
{
Console.WriteLine(city);
}
Console.ReadLine();
}
}
}
Comments
Leave a comment