How to print alphabets in C
Reasoning for printing alphabets from a to z
Printing alphabets in C is a simple task. It is a simple drill if you are familiar with fundamental data types and literals.
An ASCII character code represents every character in C. ASCII is a fixed integer number that represents each global printable or non-printable character.
Step-by-step descriptive reasoning for printing alphabets.
- Declare a character variable, for example, ch.
- Start the loop by setting the loop counter variable to ch = 'a' and increase it by one for each iteration until ch <= 'z'. The Loop should be structured as (ch='a'; ch='z'; ch++).
- Print the value of ch within the loop body.
A program for printing the alphabet from A to Z.
#include <stdio.h>
int main()
{
char ch;
printf("Alphabets from A to Z are: \n");
for(ch='A'; ch<='Z'; ch++)
{
printf(" %c ", ch);
}
return 0;
Output:
To demonstrate that characters are internally encoded as integers. Now, using the ASCII values, print all alphabets.
A program that displays alphabets using ASCII data
#include <stdio.h>
int main()
{
int i;
printf("Alphabets from A - Z are: \n");
/* ASCII value of A=65 */
for(i=65; i<=90; i++)
{
/*
* Integer i with %c will convert integer
* to character before printing. %c will
* read ascii from i and display the character
* similar.
*/
printf(" %c ", i);
}
return 0;
}
Output:
Program for printing alphabets from A to L.
// C application to display Alphabets from A to L.
#include <stdio.h>
void main()
{
char ch; // Variable declaration
for (ch = 'A'; ch <= 'L'; ch++)
{
printf("%c ", ch);
}
}
Output:
This program used a for Loop to run through the characters from 'A' to 'L'. Within the Loop, the printf command displays each character, preceded by a space.
Print the alphabets in lowercase and uppercase
#include <stdio.h>
int main() {
char c;
printf("Enter f to view uppercase alphabets.\n");
printf("Enter m to view lowercase alphabets. \n");
scanf("%c", &c);
if (c == 'f' || c == 'f') {
for (c = 'A'; c <= 'Z'; ++c)
printf("%c ", c);
} else if (c == 'L' || c == 'm') {
for (c = 'a'; c <= 'z'; ++c)
printf("%c ", c);
} else {
printf("Error! You entered an invalid character.");
}
return 0;
}
Output:
Print Alphabets from A to Z using the For Loop
This program uses a for loop to output the alphabet from A to Z. Now, For Loop will ensure that the character (ch) is between a and z
#include <stdio.h>
int main()
{
char ch;
printf("\n List of letters from a to z are : \n");
for(ch = 'a'; ch <= 'z'; ch++)
{
printf(" %c ", ch);
}
return 0;
}
Output:
C program that displays the alphabet from a to z using ASCII codes.
The ASCII codes are used in this application to print alphabets from a to z. To learn the ASCII values of each character in C Programming, I recommend consulting the ASCII Table.
#include <stdio.h>
int main()
{
int i;
printf("\n List of letters from a to z are : \n");
for(i = 97; i <= 122; i++)
{
printf(" %c", i);
}
return 0;
}
Output:
C Program to Print Alphabets from A to Z using While loop
This alphabet program is the same as the one described above. We just changed the For Loop to a While Loop.
#include <stdio.h>
int main()
{
char ch = 'a';
printf("\n List of Alphabets from a to z are : \n");
while(ch <= 'z')
{
printf(" %c", ch);
ch++;
}
return 0;
}
Output:
Program to Print Alphabets Repeated in Times
#include<stdio.h>
#include<stdlib.h>
int main()
{
int n, i;
char c;
printf("Enter n: ");
scanf("%d", &n);
// the outer Loop executes n times
for (i = 0; i < n; ++i) {
for (c = 'a'; c <= 'k'; ++c) {
printf("%c ", c);
}
printf("\n");
}
}
Output:
A Recursion-Based Program to Print Every English Alphabet n Times
Each recursive call prints the entire alphabet from 'A' to 'Z' once. For every recursive call, we will decrement n When the value of n approaches zero, the recursive call ends. In this way, the alphabet is printed n times.
#include<stdio.h>
#include<stdlib.h>
void PrintAlphabets (int n)
{
if (n <= 0)
return;
char c;
for (c = 'a'; c <= 'z'; ++c) {
printf("%c ", c);
}
printf("\n");
PrintAlphabets(n - 1);
}
int main()
{
int n, i;
char c;
printf("Enter n: ");
scanf("%d", &n);
PrintAlphabets(n);
}
Output: