Wednesday, May 29, 2013

How to convert Number to Roman Numericals in C#


In the Below post V` means=5000,
                            VM` =6000,
                            MC`=9000.


public partial class NumberConversion : Form
    {
        public string[] strOnes = { "", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX" };
        public string[] strTens = { "", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC" };
        public string[] strHundreds = { "", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM" };
        public string[] strThousands = { "", "M", "MM", "MMM", "MMMM","V`","VM`","VMM`","VMMM`","MC`" };
        int intOnes, intTens, intHundrs;
        public string strRomanNum;

        public NumberConversion()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (txtNumber.TextLength < 5)
            {
                int intNum = Convert.ToInt32(txtNumber.Text);
                int intlength = intNum.ToString().Length;
             
                    int intTh = intNum / 1000;
                    intNum = intNum % 1000;


                    int intH = intNum / 100;
                    intHundrs = intNum % 100;

                    int intTe = intHundrs / 10;
                    intTens = intHundrs % 10;


                    intOnes = intTens;

                    strRomanNum = strThousands[intTh] + strHundreds[intH] + strTens[intTe] + strOnes[intOnes];
                    txtRomanNumber.Text = strRomanNum;            
            }
            else
            {
                MessageBox.Show("please enter the vlaue lessthan 9999");
                txtNumber.Clear();
                txtRomanNumber.Clear();
            }
        }
  }

output::::

No comments:

Post a Comment