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

Question #180350

Write a program using standard string functions that accepts a price of an item and display its coded value. The base of the key is: X C O M P U T E R S 1 2 3 4 5 6 7 8 9 Sample input/output dialogue: Enter price: 489.50 Coded value: PRS.UX


1
Expert's answer
2021-04-11T13:04:12-0400
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace Q180350
{
    class Program
    {
       
        static void Main(string[] args)
        {
            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-us");
            string codedValues = "XCOMPUTERS";
            Console.Write("Enter price: ");
            //read price
            string price = Console.ReadLine();
            string codedValue="";
            double dblPrice=0;
            try
            {
                if (double.TryParse(price, out dblPrice))
                {
                    ///convert price to the coded value
                    for (int i = 0; i < price.Length; i++)
                    {
                        if (price[i] != '.')
                        {
                            codedValue += codedValues[int.Parse(price[i].ToString())].ToString();
                        }
                        else
                        {
                            codedValue += ".";
                        }
                    }
                    //display the coded value
                    Console.WriteLine("Coded value: {0}", codedValue);
                }
                else
                {
                    Console.WriteLine("Wrong price.");
                }
            }
            catch (Exception ex) {
                Console.WriteLine("Wrong price.");
            }
            Console.ReadLine();
        }
    }
}

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