In this article, we are going to learn about how to Upper Case Character Pattern in C++ using ASCII and maths numbers. Pattern programs are a good exercise to master the loops in the C++ language. Here in this example, we will try to print two types of patterns.
- Print a square matrix of Upper Case characters Horizontally.
- Print a square matrix box of Upper Case characters Vertically.
1. Print a square matrix box of Upper Case characters Horizontally
In this example, we are printing a square box pattern by using upper case characters. We will print characters horizontally n number of times and then switch to the next line with a new character. The ASCII value of the Upper case characters starts at 65. ‘A’ has the value of 65 in ASCII.
Desired Output for Size 3:
A A A
B B B
C C C
#include <iostream>
using namespace std;
int main()
{
int num;
cout<< "Please enter the size of Square Box: ";
cin>> num;
for(int row=1; row<=num; row++)
{
for(int col=1; col<=num; col++)
{
cout<< char(row+64);
}
cout<<"\n";
}
return 0;
}
Actual Output:
Please enter the size of Square Box: 4
AAAA
BBBB
CCCC
DDDD
2. Print a square matrix box of Upper Case characters Horizontally
#include <iostream>
using namespace std;
int main()
{
int num;
cout<< "Please enter the size of Square Box: ";
cin>> num;
for(int row=1; row<=num; row++)
{
for(int col=1; col<=num; col++)
{
cout<< char(row+64);
}
cout<<"\n";
}
return 0;
}
Actual Output:
Please enter the size of Square Box: 4
AAAA
BBBB
CCCC
DDDD
3. Print a square matrix box of lower Case characters Vertically
In this example, we are printing a square box pattern by using upper case characters. We will print characters Vertically n number of times.
The ASCII value of the Upper case characters starts at 65. ‘A’ has the value of 65 in ASCII.
Desired Output for Size 3:
A B C
A B C
A B C
#include <iostream>
using namespace std;
int main()
{
int num;
cout<<"Please enter the size of Square Box: ";
cin>>num;
for(int row=1; row<=num; row++)
{
for(int col=1; col<=num; col++)
{
cout<< char(col+64);
}
cout<<"\n";
}
return 0;
}
Actual Output:
Please enter the size of Square Box: 4
ABCD
ABCD
ABCD
ABCD
We can do the same pattern by using the lower case characters. The ASCII value of the Upper case characters starts at 97. ‘a’ has the value of 97 in ASCII.
#include <iostream>
using namespace std;
int main()
{
int num;
cout<<"Please enter the size of Square Box: ";
cin>>num;
for(int row=1; row<=num; row++)
{
for(int col=1; col<=num; col++)
{
cout<< char(col+96);
}
cout<<"\n";
}
return 0;
}
Output:
Please enter the size of Square Box: 4
abcd
abcd
abcd
abcd