In: Computer Science
Write a C++
Program to print the first letter of your first and last name using
stars.
Note:
1) Using nested For
Loop
2) The number of lines is
given by user.
3) Using one Outer loop to
print your letters.
4) Print the letters
beside each other
//Assuming that First and last letter of your name are M and K
#include <iostream>
using namespace std;
int abs(int d)
{
return d < 0 ? -1 * d : d;
}
// Function to print the pattern of 'A'
void printA(int height,int width)
{
int n = width / 2, i, j;
for (i = 0; i < height; i++) {
for (j = 0; j <= width; j++)
{
if (j == n || j
== (width - n)
|| (i == height / 2 && j > n
&& j < (width -
n)))
cout<<"*";
else
cout<<" ";
}
cout<<endl;
n--;
}
}
// Function to print the pattern of 'B'
void printB(int height,int width)
{
int i, j, half = (height / 2);
for (i = 0; i < height; i++) {
cout<<"*";
for (j = 0; j < width; j++)
{
if ((i == 0 || i
== height - 1 || i == half)
&& j < (width - 2))
cout<<"*";
else if (j ==
(width - 2)
&& !(i == 0 || i ==
height - 1
|| i ==
half))
cout<<"*";
else
cout<<" ";
}
cout<<endl;
}
}
// Function to print the pattern of 'C'
void printC(int height,int width)
{
int i, j;
for (i = 0; i < height; i++) {
cout<<"*";
for (j = 0; j < (height - 1);
j++) {
if (i == 0 || i
== height - 1)
cout<<"*";
else
continue;
}
cout<<endl;
}
}
// Function to print the pattern of 'D'
void printD(int height,int width)
{
int i, j;
for (i = 0; i < height; i++) {
cout<<"*";
for (j = 0; j < height; j++)
{
if ((i == 0 || i
== height - 1)
&& j < height - 1)
cout<<"*";
else if (j ==
height - 1 && i != 0
&& i != height -
1)
cout<<"*";
else
cout<<" ";
}
cout<<endl;
}
}
// Function to print the pattern of 'E'
void printE(int height,int width)
{
int i, j;
for (i = 0; i < height; i++) {
cout<<"*";
for (j = 0; j < height; j++)
{
if ((i == 0 || i
== height - 1)
|| (i == height / 2
&& j <= height /
2))
cout<<"*";
else
continue;
}
cout<<endl;
}
}
// Function to print the pattern of 'F'
void printF(int height,int width)
{
int i, j;
for (i = 0; i < height; i++) {
cout<<"*";
for (j = 0; j < height; j++)
{
if ((i == 0) ||
(i == height / 2
&& j <= height / 2))
cout<<"*";
else
continue;
}
cout<<endl;
}
}
// Function to print the pattern of 'G'
void printG(int height,int width)
{
int i, j;
width--;
for (i = 0; i < height; i++) {
for (j = 0; j < width; j++)
{
if ((i == 0 || i
== height - 1)
&& (j == 0 || j == width - 1))
cout<<" ";
else if (j ==
0)
cout<<"*";
else if (i == 0
&& j <= height)
cout<<"*";
else if (i ==
height / 2
&& j > height /
2)
cout<<"*";
else if (i >
height / 2
&& j == width -
1)
cout<<"*";
else if (i ==
height - 1
&& j <
width)
cout<<"*";
else
cout<<" ";
}
cout<<endl;
}
}
// Function to print the pattern of 'H'
void printH(int height,int width)
{
int i, j;
for (i = 0; i < height; i++) {
cout<<"*";
for (j = 0; j < height; j++)
{
if ((j == height
- 1)
|| (i == height / 2))
cout<<"*";
else
cout<<" ";
}
cout<<endl;
}
}
// Function to print the pattern of 'I'
void printI(int height,int width)
{
int i, j;
for (i = 0; i < height; i++) {
for (j = 0; j < height; j++)
{
if (i == 0 || i
== height - 1)
cout<<"*";
else if (j ==
height / 2)
cout<<"*";
else
cout<<" ";
}
cout<<endl;
}
}
// Function to print the pattern of 'J'
void printJ(int height,int width)
{
int i, j;
for (i = 0; i < height; i++) {
for (j = 0; j < height; j++)
{
if (i == height
- 1 && (j > 0
&&
j < height - 1))
cout<<"*";
else if ((j ==
height - 1
&& i != height -
1)
|| (i > (height / 2) -
1
&&
j == 0 && i != height - 1))
cout<<"*";
else
cout<<" ";
}
cout<<endl;
}
}
// Function to print the pattern of 'K'
void printK(int height,int width)
{
int i, j, half = height / 2, dummy = half;
for (i = 0; i < height; i++) {
cout<<"*";
for (j = 0; j <= half; j++)
{
if (j ==
abs(dummy))
cout<<"*";
else
cout<<" ";
}
cout<<endl;
dummy--;
}
}
// Function to print the pattern of 'L'
void printL(int height,int width)
{
int i, j;
for (i = 0; i < height; i++) {
cout<<"*";
for (j = 0; j <= height; j++)
{
if (i == height
- 1)
cout<<"*";
else
cout<<" ";
}
cout<<endl;
}
}
// Function to print the pattern of 'M'
void printM(int height,int width)
{
int i, j, counter = 0;
for (i = 0; i < height; i++) {
cout<<"*";
for (j = 0; j <= height; j++)
{
if (j ==
height)
cout<<"*";
else if (j ==
counter
|| j == height - counter -
1)
cout<<"*";
else
cout<<" ";
}
if (counter == height / 2) {
counter =
-99999;
}
else
counter++;
cout<<endl;
}
}
// Function to print the pattern of 'N'
void printN(int height,int width)
{
int i, j, counter = 0;
for (i = 0; i < height; i++) {
cout<<"*";
for (j = 0; j <= height; j++)
{
if (j ==
height)
cout<<"*";
else if (j ==
counter)
cout<<"*";
else
cout<<" ";
}
counter++;
cout<<endl;
}
}
// Function to print the pattern of 'O'
void printO(int height,int width)
{
int i, j, space = (height / 3);
width = height / 2 + height / 5 + space + space;
for (i = 0; i < height; i++) {
for (j = 0; j <= width; j++)
{
if (j == width -
abs(space)
|| j == abs(space))
cout<<"*";
else if ((i ==
0
|| i == height - 1)
&& j >
abs(space)
&& j < width -
abs(space))
cout<<"*";
else
cout<<" ";
}
if (space != 0
&& i
< height / 2) {
space--;
}
else if (i >= (height / 2 +
height / 5))
space--;
cout<<endl;
}
}
// Function to print the pattern of 'P'
void printP(int height,int width)
{
int i, j;
for (i = 0; i < height; i++) {
cout<<"*";
for (j = 0; j < height; j++)
{
if ((i == 0 || i
== height / 2)
&& j < height - 1)
cout<<"*";
else if (i <
height / 2
&& j == height - 1
&& i != 0)
cout<<"*";
else
cout<<" ";
}
cout<<endl;
}
}
// Function to print the pattern of 'Q'
void printQ(int height,int width)
{
printO(height,width);
int i, j, d = height;
for (i = 0; i < height / 2; i++) {
for (j = 0; j <= d; j++) {
if (j ==
d)
cout<<"*";
else
cout<<" ";
}
cout<<endl;
d++;
}
}
// Function to print the pattern of 'R'
void printR(int height,int width)
{
int i, j, half = (height / 2);
for (i = 0; i < height; i++) {
cout<<"*";
for (j = 0; j < width; j++)
{
if ((i == 0 || i
== half)
&& j < (width - 2))
cout<<"*";
else if (j ==
(width - 2)
&& !(i == 0 || i ==
half))
cout<<"*";
else
cout<<" ";
}
cout<<endl;
}
}
// Function to print the pattern of 'S'
void printS(int height,int width)
{
int i, j;
for (i = 0; i < height; i++) {
for (j = 0; j < height; j++)
{
if ((i == 0 || i
== height / 2
|| i == height - 1))
cout<<"*";
else if (i <
height / 2
&& j == 0)
cout<<"*";
else if (i >
height / 2
&& j == height -
1)
cout<<"*";
else
cout<<" ";
}
cout<<endl;
}
}
// Function to print the pattern of 'T'
void printT(int height,int width)
{
int i, j;
for (i = 0; i < height; i++) {
for (j = 0; j < height; j++)
{
if (i ==
0)
cout<<"*";
else if (j ==
height / 2)
cout<<"*";
else
cout<<" ";
}
cout<<endl;
}
}
// Function to print the pattern of 'U'
void printU(int height,int width)
{
int i, j;
for (i = 0; i < height; i++) {
if (i != 0 && i != height -
1)
cout<<"*";
else
cout<<"
";
for (j = 0; j < height; j++)
{
if (((i ==
height - 1)
&& j >= 0
&& j < height - 1))
cout<<"*";
else if (j ==
height - 1 && i != 0
&& i != height -
1)
cout<<"*";
else
cout<<" ";
}
cout<<endl;
}
}
// Function to print the pattern of 'V'
void printV(int height,int width)
{
int i, j, counter = 0;
for (i = 0; i < height; i++) {
for (j = 0; j <= width; j++)
{
if (j ==
counter
|| j == width - counter - 1)
cout<<"*";
else
cout<<" ";
}
counter++;
cout<<endl;
}
}
// Function to print the pattern of 'W'
void printW(int height,int width)
{
int i, j, counter = height / 2;
for (i = 0; i < height; i++) {
cout<<"*";
for (j = 0; j <= height; j++)
{
if (j ==
height)
cout<<"*";
else if ((i
>= height / 2)
&& (j ==
counter
|| j ==
height - counter - 1))
cout<<"*";
else
cout<<" ";
}
if (i >= height / 2) {
counter++;
}
cout<<endl;
}
}
// Function to print the pattern of 'X'
void printX(int height,int width)
{
int i, j, counter = 0;
for (i = 0; i <= height; i++) {
for (j = 0; j <= height; j++)
{
if (j ==
counter
|| j == height - counter)
cout<<"*";
else
cout<<" ";
}
counter++;
cout<<endl;
}
}
// Function to print the pattern of 'Y'
void printY(int height,int width)
{
int i, j, counter = 0;
for (i = 0; i < height; i++) {
for (j = 0; j <= height; j++)
{
if (j ==
counter
|| j == height - counter
&& i <= height /
2)
cout<<"*";
else
cout<<" ";
}
cout<<endl;
if (i < height / 2)
counter++;
}
}
// Function to print the pattern of 'Z'
void printZ(int height,int width)
{
int i, j, counter = height - 1;
for (i = 0; i < height; i++) {
for (j = 0; j < height; j++)
{
if (i == 0 || i
== height - 1
|| j == counter)
cout<<"*";
else
cout<<" ";
}
counter--;
cout<<endl;
}
}
// Function print the pattern of the
// alphabets from A to Z
void printPattern(char character,int height,int width)
{
switch (character) {
case 'A':
printA(height,width);
break;
case 'B':
printB(height,width);
break;
case 'C':
printC(height,width);
break;
case 'D':
printD(height,width);
break;
case 'E':
printE(height,width);
break;
case 'F':
printF(height,width);
break;
case 'G':
printG(height,width);
break;
case 'H':
printH(height,width);
break;
case 'I':
printI(height,width);
break;
case 'J':
printJ(height,width);
break;
case 'K':
printK(height,width);
break;
case 'L':
printL(height,width);
break;
case 'M':
printM(height,width);
break;
case 'N':
printN(height,width);
break;
case 'O':
printO(height,width);
break;
case 'P':
printP(height,width);
break;
case 'Q':
printQ(height,width);
break;
case 'R':
printR(height,width);
break;
case 'S':
printS(height,width);
break;
case 'T':
printT(height,width);
break;
case 'U':
printU(height,width);
break;
case 'V':
printV(height,width);
break;
case 'W':
printW(height,width);
break;
case 'X':
printX(height,width);
break;
case 'Y':
printY(height,width);
break;
case 'Z':
printZ(height,width);
break;
}
}
// Driver Code
int main()
{
int height;
char first_name_initial = 'M';
char second_name_initial = 'K';
cout<<"Enter number of lines\n";
cin>>height;
int width = (2 * height) - 1;
printPattern(first_name_initial,height,width);
printPattern(second_name_initial,height,width);
return 0;
}