YouTip LogoYouTip

C Examples Display Alphabets

# C Example - Loop to Print 26 Alphabets [![Image 3: C Examples](#) C Examples](#) Loop to print 26 alphabets. In the following example, we use the variable `letter` to store the current alphabet to be printed. Then, we use a `for` loop to repeat printing the alphabet 26 times, adding a space after each letter. Inside the loop, we use the `printf` function to output the value of the `letter` variable. `%c` is the format specifier in `printf` used to output a character. ## Example 1 #includeint main(){char letter = 'A'; // The ASCII value of 'A' is 65// Use a loop to print 26 letters for(int i = 0; i<26; i++){printf("%c ", letter); letter++; // Increment the value of letter to get the ASCII code of the next letter}printf("n"); return 0; } Another example: ## Example 2 #includeint main(){char c; for(c = 'A'; c<= 'Z'; ++c)printf("%c ", c); return 0; } Output: 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 If you want to print lowercase letters, simply set the initial value of the variable `letter` to `'a'` (the ASCII value of `'a'` is 97), and then use `letter++` inside the loop: ## Example #includeint main(){char letter = 'a'; // The ASCII value of 'a' is 97// Use a loop to print 26 lowercase letters for(int i = 0; i<26; i++){printf("%c ", letter); letter++; // Increment the value of letter to get the ASCII code of the next letter}printf("n"); return 0; } Output: 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 The following example allows you to choose between printing uppercase or lowercase letters: ## Example - Print Uppercase or Lowercase Letters #includeint main(){char c; printf("Enter 'u' to display uppercase letters, 'l' to display lowercase letters: "); scanf("%c", &c); if(c== 'U' || c== 'u'){for(c = 'A'; c<= 'Z'; ++c)printf("%c ", c); }else if(c == 'L' || c == 'l'){for(c = 'a'; c<= 'z'; ++c)printf("%c ", c); }else printf("Error! Invalid character entered."); return 0; } Output: Enter 'u' to display uppercase letters, 'l' to display lowercase letters: l 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 [![Image 4: C Examples](#) C Examples](#) [](#)(#) (#)[](#)
← C Examples Reverse NumberC Examples Hcf Gcd β†’