Answer to Question #182735 in C# for Patrick Ian Kyle Mabunay

Question #182735

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


1
Expert's answer
2021-04-18T11:22:41-0400
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;
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS