C# programs to display uppercase letters from A to Z or lowercase letters from a to z have been shown here. Both the iterative and recursive approaches have been covered.
1. C# program & output to Display Characters from A to Z
1.1. C# program & output to Display Characters from A to Z using Iteration
/************************************* alphabetacoder.com C# Program to display characters from A to Z or a to z using iteration **************************************/ using System; namespace DisplayAtoZ { class Program { static void Main(string[] args) { // declare variables int i, s; // take input Console.WriteLine("Enter 1 to display A to Z"); Console.WriteLine("Enter 2 to display a to z"); Console.Write("Your choice >> "); s = Convert.ToInt32(Console.ReadLine()); // display result if (s == 1) { // display uppercase letters for (i = 'A'; i <= 'Z'; i++) { Console.Write((char) i + " "); } } else if (s == 2) { // display lowercase letters for (i = 'a'; i <= 'z'; i++) { Console.Write((char) i + " "); } } else Console.Write("Wrong choice!");; // new line Console.WriteLine(""); // wait for the user to press any key Console.ReadKey(); } } }
Output
Case 1:
Enter 1 to display A to Z
Enter 2 to display a to z
Your choice >> 1
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Case 2:
Enter 1 to display A to Z
Enter 2 to display a to z
Your choice >> 2
a b c d e f g h i j k l m n o p q r s t u v w x y z
Case 3:
Enter 1 to display A to Z
Enter 2 to display a to z
Your choice >> 3
Wrong choice!
1.2. C# program & output to Display Characters from A to Z using Recursion
/************************************ alphabetacoder.com C# Program to display characters from A to Z or a to z using recursion *************************************/ using System; namespace DisplayAtoZ { class Program { // recursive function to print A to Z static void print_uppercase(char i, char j) { // display current character Console.Write(i + " "); // exit condition if (i == j) return; // call function print_uppercase(++i, j); } // recursive function to print a to z static void print_lowercase(char i, char j) { // display current character Console.Write(i + " "); // exit condition if (i == j) return; // call function print_lowercase(++i, j); } static void Main(string[] args) { // declare variables int s; // take input Console.WriteLine("Enter 1 to display A to Z"); Console.WriteLine("Enter 2 to display a to z"); Console.Write("Your choice >> "); s = Convert.ToInt32(Console.ReadLine()); // display result if (s == 1) { // display uppercase letters // by calling recursive function print_uppercase('A', 'Z'); } else if (s == 2) { // display lowercase letters // by calling recursive function print_lowercase('a', 'z'); } else Console.Write("Wrong choice!");; // new line Console.WriteLine(""); // wait for the user to press any key Console.ReadKey(); } } }
Output
Case 1:
Enter 1 to display A to Z
Enter 2 to display a to z
Your choice >> 1
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Case 2:
Enter 1 to display A to Z
Enter 2 to display a to z
Your choice >> 2
a b c d e f g h i j k l m n o p q r s t u v w x y z
Case 3:
Enter 1 to display A to Z
Enter 2 to display a to z
Your choice >> 0
Wrong choice!