Write a program using string functions that will accept the name of the country as input value and will display the corresponding capital. Here is the list of the countries and their capitals.
COUNTRY CAPITAL
Canada Ottawa
United States Washington D.C.
U.S.S.R. Moscow
Italy Rome
Philippines Manila
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Q182735
{
class Program
{
/// <summary>
/// Main method
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
//accept the name of the country as input value
Console.Write("Enter the name of the country: ");
string countryName = Console.ReadLine();
string capital = getCapital(countryName);
if (capital == null)
{
Console.WriteLine("\nThe capital is not found for this country.\n");
}
else {
// display the corresponding capital
Console.WriteLine("The capital is {0}", countryName, capital);
}
Console.ReadLine();
}
/// <summary>
/// Gets capital
/// </summary>
/// <param name="countryName"></param>
/// <returns></returns>
static string getCapital(string countryName)
{
string[] countries = new string[] { "Canada", "United States", "U.S.S.R.", "Italy", "Philippines" };
string[] capitals = new string[] { "Ottawa", "Washington D.C.", "Moscow", "Rome", "Manila" };
for (int i = 0; i < countries.Length; i++)
{
if (countries[i] == countryName)
{
return capitals[i];
}
}
return null;
}
}
}
Comments
Leave a comment