Program to Print Alphabets From A to Z Using Loop
Last Updated :
13 Dec, 2023
Our task is to print the alphabets from A to Z using loops. There are various methods to print alphabets from (A to Z) or (a to z).
- Using ASCII values
- Using character variables.
In this article we will mainly focus on the following programs and their logic:
- Using for loop
- Using the while loop
- Using a do-while loop
Program to display alphabets using ASCII values
C++
#include <iostream>
using namespace std;
int main()
{
int i;
cout << "Alphabets from (A-Z) are:\n" ;
for (i = 65; i <= 90; i++) {
cout << ( char )i << " " ;
}
cout << "\nAlphabets from (a-z) are:\n" ;
for (i = 97; i <= 122; i++) {
cout << ( char )i << " " ;
}
return 0;
}
|
C
#include <stdio.h>
int main()
{
int i;
printf ( "Alphabets from (A-Z) are:\n" );
for (i = 65; i <= 90; i++) {
printf ( "%c " , i);
}
printf ( "\nAlphabets from (a-z) are:\n" );
for (i = 97; i <= 122; i++) {
printf ( "%c " , i);
}
return 0;
}
|
Java
public class Main {
public static void main(String[] args) {
int i;
System.out.println( "Alphabets from (A-Z) are:" );
for (i = 65 ; i <= 90 ; i++) {
System.out.print(( char )i + " " );
}
System.out.println( "\nAlphabets from (a-z) are:" );
for (i = 97 ; i <= 122 ; i++) {
System.out.print(( char )i + " " );
}
}
}
|
Python3
def main():
print ( "Alphabets from (A-Z) are:" )
for i in range ( 65 , 91 ):
print ( chr (i), end = " " )
print ( "\nAlphabets from (a-z) are:" )
for i in range ( 97 , 123 ):
print ( chr (i), end = " " )
if __name__ = = "__main__" :
main()
|
C#
using System;
public class GFG
{
public static void Main()
{
int i;
Console.WriteLine( "Alphabets from (A-Z) are:" );
for (i = 65; i <= 90; i++)
{
Console.Write(( char )i + " " );
}
Console.WriteLine( "\nAlphabets from (a-z) are:" );
for (i = 97; i <= 122; i++)
{
Console.Write(( char )i + " " );
}
}
}
|
Javascript
console.log( "Alphabets from (A-Z) are:" );
for (let i = 65; i <= 90; i++) {
console.log(String.fromCharCode(i) + " " );
}
console.log( "\nAlphabets from (a-z) are:" );
for (let i = 97; i <= 122; i++) {
console.log(String.fromCharCode(i) + " " );
}
|
Output
Alphabets from (A-Z) are:
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
Alphabets from (a-z) are:
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
Program to print (A to Z) and (a to z) using for loop
In the below program,
- For loop is used to print the alphabets from A to Z. A loop variable is taken to do this of type ‘char’.
- The loop variable ‘i’ is initialized with the first alphabet ‘A’ and incremented by 1 on every iteration.
- In the loop, the character ‘i’ is printed as the alphabet.
Program:
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
char i;
cout << "The Alphabets from A to Z are: \n" ;
for (i = 'A' ; i <= 'Z' ; i++) {
cout << i << " " ;
}
cout << "\nThe Alphabets from a to z are: \n" ;
for (i = 'a' ; i <= 'z' ; i++) {
cout << i << " " ;
}
return 0;
}
|
C
#include <stdio.h>
int main()
{
char i;
printf ( "The Alphabets from A to Z are: \n" );
for (i = 'A' ; i <= 'Z' ; i++) {
printf ( "%c " , i);
}
printf ( "\nThe Alphabets from a to z are: \n" );
for (i = 'a' ; i <= 'z' ; i++) {
printf ( "%c " , i);
}
return 0;
}
|
Java
class GFG {
public static void main(String[] args)
{
char i;
System.out.printf( "The Alphabets from A to Z are: \n" );
for (i = 'A' ; i <= 'Z' ; i++) {
System.out.printf( "%c " , i);
}
System.out.printf( "\nThe Alphabets from a to z are: \n" );
for (i = 'a' ; i <= 'z' ; i++) {
System.out.printf( "%c " , i);
}
}
}
|
Python3
if __name__ = = '__main__' :
i = chr ;
print ( "The Alphabets from A to Z are: " );
for i in range ( ord ( 'A' ), ord ( 'Z' ) + 1 ):
print ( chr (i), end = " " );
print ( "\nThe Alphabets from a to z are: " );
for i in range ( ord ( 'a' ), ord ( 'z' ) + 1 ):
print ( chr (i), end = " " );
|
C#
using System;
class GFG
{
public static void Main(String[] args)
{
char i;
Console.Write( "The Alphabets from A to Z are: \n" );
for (i = 'A' ; i <= 'Z' ; i++)
{
Console.Write( "{0} " , i);
}
Console.Write( "\nThe Alphabets from a to z are: \n" );
for (i = 'a' ; i <= 'z' ; i++)
{
Console.Write( "{0} " , i);
}
}
}
|
Javascript
<script>
let i;
document.write( "The Alphabets from A" +
" to Z are: " + "</br>" );
for (i = 'A' .charCodeAt();
i <= 'Z' .charCodeAt(); i++)
{
document.write(
String.fromCharCode(i) + " " );
}
document.write( "The Alphabets from a" +
" to z are: " + "</br>" );
for (i = 'a' .charCodeAt();
i <= 'z' .charCodeAt(); i++)
{
document.write(
String.fromCharCode(i) + " " );
}
</script>
|
Output
The Alphabets from A to Z are:
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 Alphabets from a to z are:
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
Program to print (A to Z) and (a to z) using the while loop
In the below program,
- While loop is used to print the alphabets from A to Z. A loop variable is taken to display of type ‘char’.
- The loop variable ‘i’ is initialized with the first alphabet ‘A’ and incremented by 1 on every iteration.
- In the loop, the character ‘i’ is printed as the alphabet.
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
char i;
cout << "The Alphabets from A to Z are: \n" ;
i = 'A' ;
while (i <= 'Z' ) {
cout << i << ' ' ;
i++;
}
i = 'a' ;
cout << "\nThe Alphabets from a to z are: \n" ;
while (i <= 'z' ) {
cout << i << ' ' ;
i++;
}
return 0;
}
|
C
#include <stdio.h>
int main()
{
char i;
printf ( "The Alphabets from A to Z are: \n" );
i = 'A' ;
while (i <= 'Z' ) {
printf ( "%c " , i);
i++;
}
printf ( "\nThe Alphabets from a to z are: \n" );
i = 'a' ;
while (i <= 'z' ) {
printf ( "%c " , i);
i++;
}
return 0;
}
|
Java
import java.io.*;
public class GFG {
public static void main(String[] args) {
char i;
System.out.println( "The Alphabets from A to Z are: " );
i = 'A' ;
while (i <= 'Z' ) {
System.out.print(i + " " );
i++;
}
i = 'a' ;
System.out.println( "\nThe Alphabets from a to z are: " );
while (i <= 'z' ) {
System.out.print(i + " " );
i++;
}
}
}
|
Python
i = 'A'
print "The Alphabets from A to Z are:"
while ord (i) < = ord ( 'Z' ):
print i,
i = chr ( ord (i) + 1 )
i = 'a'
print "\nThe Alphabets from a to z are:"
while ord (i) < = ord ( 'z' ):
print i,
i = chr ( ord (i) + 1 )
|
C#
using System;
class Program {
static void Main()
{
char i;
Console.WriteLine( "The Alphabets from A to Z are:" );
i = 'A' ;
while (i <= 'Z' ) {
Console.Write(i + " " );
i++;
}
i = 'a' ;
Console.WriteLine(
"\nThe Alphabets from a to z are:" );
while (i <= 'z' ) {
Console.Write(i + " " );
i++;
}
}
}
|
Javascript
process.stdout.write( "The Alphabets from A to Z are: \n" );
for (let i = 65; i <= 90; i++) {
process.stdout.write(String.fromCharCode(i) + " " );
}
process.stdout.write( "\nThe Alphabets from a to z are: \n" );
for (let i = 97; i <= 122; i++) {
process.stdout.write(String.fromCharCode(i) + " " );
}
|
Output
The Alphabets from A to Z are:
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 Alphabets from a to z are:
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
Program to print (A to Z) and (a to z) using a do-while loop
In the below program,
- The do-while loop is used to print the alphabets from A to Z. A loop variable is taken to display of type ‘char’.
- The loop variable ‘i’ is initialized with the first alphabet ‘A’ and incremented by 1 on every iteration.
- In the loop, the character ‘i’ is printed as the alphabet.
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
char i;
cout << "The Alphabets from A to Z are: \n" ;
i = 'A' ;
do {
cout << i << ' ' ;
i++;
} while (i <= 'Z' );
i = 'a' ;
cout << "\nThe Alphabets from a to z are: \n" ;
do {
cout << i << ' ' ;
i++;
} while (i <= 'z' );
return 0;
}
|
C
#include <stdio.h>
int main()
{
char i;
printf ( "The Alphabets from A to Z are: \n" );
i = 'A' ;
do {
printf ( "%c " , i);
i++;
} while (i <= 'Z' );
printf ( "\nThe Alphabets from a to z are: \n" );
i = 'a' ;
do {
printf ( "%c " , i);
i++;
} while (i <= 'z' );
return 0;
}
|
Java
public class AlphabetDisplay {
public static void main(String[] args) {
char i;
System.out.println( "The Alphabets from A to Z are:" );
i = 'A' ;
do {
System.out.print(i + " " );
i++;
} while (i <= 'Z' );
System.out.println( "\nThe Alphabets from a to z are:" );
i = 'a' ;
do {
System.out.print(i + " " );
i++;
} while (i <= 'z' );
}
}
|
Python3
print ( "The Alphabets from A to Z are: " )
i = 'A'
while True :
print (i, end = ' ' )
i = chr ( ord (i) + 1 )
if i > 'Z' :
break
print ( "\nThe Alphabets from a to z are: " )
i = 'a'
while True :
print (i, end = ' ' )
i = chr ( ord (i) + 1 )
if i > 'z' :
break
|
C#
using System;
class Program
{
static void Main()
{
Console.WriteLine( "The Alphabets from A to Z are:" );
char i = 'A' ;
do
{
Console.Write(i + " " );
i++;
} while (i <= 'Z' );
Console.WriteLine();
Console.WriteLine( "The Alphabets from a to z are:" );
i = 'a' ;
do
{
Console.Write(i + " " );
i++;
} while (i <= 'z' );
Console.WriteLine();
}
}
|
Javascript
let i;
console.log( "The Alphabets from A to Z are:" );
i = 'A' ;
do {
process.stdout.write(i + " " );
i = String.fromCharCode(i.charCodeAt(0) + 1);
} while (i <= 'Z' );
console.log( "\nThe Alphabets from a to z are:" );
i = 'a' ;
do {
process.stdout.write(i + " " );
i = String.fromCharCode(i.charCodeAt(0) + 1);
} while (i <= 'z' );
|
Output
The Alphabets from A to Z are:
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 Alphabets from a to z are:
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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...