Convert Number to Alphabet

rudalboy09

Member
Joined
Nov 27, 2014
Messages
15
Programming Experience
1-3
i want to change number to alphabet like this
if i put number "1". the result is "A"
when i put number "5". the result is "E"
when i put number "26". the result is "Z"
and then when i put number "27". the result is "A1"
and then when i put number "53". the result is "B1"

please answer this.
 
What are the rules? You've provided some examples and we can glean important information from them but that still doesn't give us the general case. Looking at the first three examples, I can see that, given a number N that is in the range 1 to 26, you want the Nth letter of the alphabet. Looking at the fourth example, I would then have said that, for a number N greater than 26, you would divide N by 26 and use the remainder as in the first three examples and use the quotient as a suffix, but the fifth example blows that theory out of the water. If you want to be able to write code to do something like this then you have to have a clear set of rules to follow in order to get the desired answer. If you haven't done that then you can't possibly write the code.
 
angka1 = Convert.ToInt32(dataGridView1.Columns.Count);
if (angka1 > 26)
{
int pembantuhasil;
hasil2 = angka1 % 26;
pembantuhasil = angka1 / 26;
#region hasil1
if (pembantuhasil == 1)
{
hasil1 = "A";
}
if (pembantuhasil == 2)
{
hasil1 = "B";
}
if (pembantuhasil == 3)
{
hasil1 = "C";
}
if (pembantuhasil == 4)
{
hasil1 = "D";
}
if (pembantuhasil == 5)
{
hasil1 = "E";
}
if (pembantuhasil == 6)
{
hasil1 = "F";
}
if (pembantuhasil == 7)
{
hasil1 = "G";
}
if (pembantuhasil == 8)
{
hasil1 = "H";
}
if (pembantuhasil == 9)
{
hasil1 = "I";
}
if (pembantuhasil == 10)
{
hasil1 = "J";
}
if (pembantuhasil == 11)
{
hasil1 = "K";
}
if (pembantuhasil == 12)
{
hasil1 = "L";
}
if (pembantuhasil == 13)
{
hasil1 = "M";
}
if (pembantuhasil == 14)
{
hasil1 = "N";
}
if (pembantuhasil == 15)
{
hasil1 = "O";
}
if (pembantuhasil == 16)
{
hasil1 = "P";
}
if (pembantuhasil == 17)
{
hasil1 = "Q";
}
if (pembantuhasil == 18)
{
hasil1 = "R";
}
if (pembantuhasil == 19)
{
hasil1 = "S";
}
if (pembantuhasil == 20)
{
hasil1 = "T";
}
if (pembantuhasil == 21)
{
hasil1 = "U";
}
if (pembantuhasil == 22)
{
hasil1 = "V";
}
if (pembantuhasil == 23)
{
hasil1 = "W";
}
if (pembantuhasil == 24)
{
hasil1 = "X";
}
if (pembantuhasil == 25)
{
hasil1 = "Y";
}
if (pembantuhasil == 26)
{
hasil1 = "Z";
}
#endregion
textBox1.Text = hasil1.ToString() + hasil2.ToString();
}
else
{
#region hasil1
if (angka1 == 1)
{
hasil1 = "A";
}
if (angka1 == 2)
{
hasil1 = "B";
}
if (angka1 == 3)
{
hasil1 = "C";
}
if (angka1 == 4)
{
hasil1 = "D";
}
if (angka1 == 5)
{
hasil1 = "E";
}
if (angka1 == 6)
{
hasil1 = "F";
}
if (angka1 == 7)
{
hasil1 = "G";
}
if (angka1 == 8)
{
hasil1 = "H";
}
if (angka1 == 9)
{
hasil1 = "I";
}
if (angka1 == 10)
{
hasil1 = "J";
}
if (angka1 == 11)
{
hasil1 = "K";
}
if (angka1 == 12)
{
hasil1 = "L";
}
if (angka1 == 13)
{
hasil1 = "M";
}
if (angka1 == 14)
{
hasil1 = "N";
}
if (angka1 == 15)
{
hasil1 = "O";
}
if (angka1 == 16)
{
hasil1 = "P";
}
if (angka1 == 17)
{
hasil1 = "Q";
}
if (angka1 == 18)
{
hasil1 = "R";
}
if (angka1 == 19)
{
hasil1 = "S";
}
if (angka1 == 20)
{
hasil1 = "T";
}
if (angka1 == 21)
{
hasil1 = "U";
}
if (angka1 == 22)
{
hasil1 = "V";
}
if (angka1 == 23)
{
hasil1 = "W";
}
if (angka1 == 24)
{
hasil1 = "X";
}
if (angka1 == 25)
{
hasil1 = "Y";
}
if (angka1 == 26)
{
hasil1 = "Z";
}
#endregion
textBox1.Text = hasil1.ToString();
}

SOLVED
 
That's not great code I'm afraid. If you don't want to provide a proper explanation of what you want though, I guess you can go with it. Consider this though:
var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

hasil1 = alphabet[angka1 - 1].ToString();
Slightly more succinct than 26 `if` statements, yes?
 
Back
Top Bottom