Print the Alphabets A to Z in Star Pattern
Given any alphabet between A to Z, the task is to print the pattern of the given alphabet using star.
Examples:
Input: A Output: ** * * ****** * * * * Input: P Output: ***** * * ***** * *
Approach: The code to print each alphabet is created in a separate function. Use switch statement to call the desired function on the basis of the alphabet’s pattern required.
Below is the implementation.
C++
// C++ implementation to print the // pattern of alphabets A to Z using * #include <iostream> using namespace std; // Below height and width variable can be used // to create a user-defined sized alphabet's pattern // Number of lines for the alphabet's pattern int height = 5; // Number of character width in each line int width = (2 * height) - 1; // Function to find the absolute value // of a number D int abs ( int d) { return d < 0 ? -1 * d : d; } // Function to print the pattern of 'A' void printA() { 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 << "\n" ; n--; } } // Function to print the pattern of 'B' void printB() { 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 << "\n" ; } } // Function to print the pattern of 'C' void printC() { 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 << "\n" ; } } // Function to print the pattern of 'D' void printD() { 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 << "\n" ; } } // Function to print the pattern of 'E' void printE() { 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 << "\n" ; } } // Function to print the pattern of 'F' void printF() { 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 << "\n" ; } } // Function to print the pattern of 'G' void printG() { 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 << "\n" ; } } // Function to print the pattern of 'H' void printH() { 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 << "\n" ; } } // Function to print the pattern of 'I' void printI() { 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 << "\n" ; } } // Function to print the pattern of 'J' void printJ() { 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 << "\n" ; } } // Function to print the pattern of 'K' void printK() { 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 << "\n" ; dummy--; } } // Function to print the pattern of 'L' void printL() { int i, j; for (i = 0; i < height; i++) { cout << "*" ; for (j = 0; j <= height; j++) { if (i == height - 1) cout << "*" ; else cout << " " ; } cout << "\n" ; } } // Function to print the pattern of 'M' void printM() { 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 << "\n" ; } } // Function to print the pattern of 'N' void printN() { 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 << "\n" ; } } // Function to print the pattern of 'O' void printO() { int i, j, space = (height / 3); int 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 << "\n" ; } } // Function to print the pattern of 'P' void printP() { 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 << "\n" ; } } // Function to print the pattern of 'Q' void printQ() { printO(); 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 << "\n" ; d++; } } // Function to print the pattern of 'R' void printR() { 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 << "\n" ; } } // Function to print the pattern of 'S' void printS() { 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 << "\n" ; } } // Function to print the pattern of 'T' void printT() { 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 << "\n" ; } } // Function to print the pattern of 'U' void printU() { 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 << "\n" ; } } // Function to print the pattern of 'V' void printV() { 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 << "\n" ; } } // Function to print the pattern of 'W' void printW() { 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 << "\n" ; } } // Function to print the pattern of 'X' void printX() { 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 << "\n" ; } } // Function to print the pattern of 'Y' void printY() { 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 << "\n" ; if (i < height / 2) counter++; } } // Function to print the pattern of 'Z' void printZ() { 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 << "\n" ; } } // Function print the pattern of the // alphabets from A to Z void printPattern( char character) { switch (character) { case 'A' : printA(); break ; case 'B' : printB(); break ; case 'C' : printC(); break ; case 'D' : printD(); break ; case 'E' : printE(); break ; case 'F' : printF(); break ; case 'G' : printG(); break ; case 'H' : printH(); break ; case 'I' : printI(); break ; case 'J' : printJ(); break ; case 'K' : printK(); break ; case 'L' : printL(); break ; case 'M' : printM(); break ; case 'N' : printN(); break ; case 'O' : printO(); break ; case 'P' : printP(); break ; case 'Q' : printQ(); break ; case 'R' : printR(); break ; case 'S' : printS(); break ; case 'T' : printT(); break ; case 'U' : printU(); break ; case 'V' : printV(); break ; case 'W' : printW(); break ; case 'X' : printX(); break ; case 'Y' : printY(); break ; case 'Z' : printZ(); break ; } } // Driver Code int main() { char character = 'A' ; printPattern(character); return 0; } // This code is contributed by shivani. |
C
// C++ implementation to print the // pattern of alphabets A to Z using * #include <stdio.h> // Below height and width variable can be used // to create a user-defined sized alphabet's pattern // Number of lines for the alphabet's pattern int height = 5; // Number of character width in each line int width = (2 * height) - 1; // Function to find the absolute value // of a number D int abs ( int d) { return d < 0 ? -1 * d : d; } // Function to print the pattern of 'A' void printA() { 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))) printf ( "*" ); else printf ( " " ); } printf ( "\n" ); n--; } } // Function to print the pattern of 'B' void printB() { int i, j, half = (height / 2); for (i = 0; i < height; i++) { printf ( "*" ); for (j = 0; j < width; j++) { if ((i == 0 || i == height - 1 || i == half) && j < (width - 2)) printf ( "*" ); else if (j == (width - 2) && !(i == 0 || i == height - 1 || i == half)) printf ( "*" ); else printf ( " " ); } printf ( "\n" ); } } // Function to print the pattern of 'C' void printC() { int i, j; for (i = 0; i < height; i++) { printf ( "*" ); for (j = 0; j < (height - 1); j++) { if (i == 0 || i == height - 1) printf ( "*" ); else continue ; } printf ( "\n" ); } } // Function to print the pattern of 'D' void printD() { int i, j; for (i = 0; i < height; i++) { printf ( "*" ); for (j = 0; j < height; j++) { if ((i == 0 || i == height - 1) && j < height - 1) printf ( "*" ); else if (j == height - 1 && i != 0 && i != height - 1) printf ( "*" ); else printf ( " " ); } printf ( "\n" ); } } // Function to print the pattern of 'E' void printE() { int i, j; for (i = 0; i < height; i++) { printf ( "*" ); for (j = 0; j < height; j++) { if ((i == 0 || i == height - 1) || (i == height / 2 && j <= height / 2)) printf ( "*" ); else continue ; } printf ( "\n" ); } } // Function to print the pattern of 'F' void printF() { int i, j; for (i = 0; i < height; i++) { printf ( "*" ); for (j = 0; j < height; j++) { if ((i == 0) || (i == height / 2 && j <= height / 2)) printf ( "*" ); else continue ; } printf ( "\n" ); } } // Function to print the pattern of 'G' void printG() { 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)) printf ( " " ); else if (j == 0) printf ( "*" ); else if (i == 0 && j <= height) printf ( "*" ); else if (i == height / 2 && j > height / 2) printf ( "*" ); else if (i > height / 2 && j == width - 1) printf ( "*" ); else if (i == height - 1 && j < width) printf ( "*" ); else printf ( " " ); } printf ( "\n" ); } } // Function to print the pattern of 'H' void printH() { int i, j; for (i = 0; i < height; i++) { printf ( "*" ); for (j = 0; j < height; j++) { if ((j == height - 1) || (i == height / 2)) printf ( "*" ); else printf ( " " ); } printf ( "\n" ); } } // Function to print the pattern of 'I' void printI() { int i, j; for (i = 0; i < height; i++) { for (j = 0; j < height; j++) { if (i == 0 || i == height - 1) printf ( "*" ); else if (j == height / 2) printf ( "*" ); else printf ( " " ); } printf ( "\n" ); } } // Function to print the pattern of 'J' void printJ() { int i, j; for (i = 0; i < height; i++) { for (j = 0; j < height; j++) { if (i == height - 1 && (j > 0 && j < height - 1)) printf ( "*" ); else if ((j == height - 1 && i != height - 1) || (i > (height / 2) - 1 && j == 0 && i != height - 1)) printf ( "*" ); else printf ( " " ); } printf ( "\n" ); } } // Function to print the pattern of 'K' void printK() { int i, j, half = height / 2, dummy = half; for (i = 0; i < height; i++) { printf ( "*" ); for (j = 0; j <= half; j++) { if (j == abs (dummy)) printf ( "*" ); else printf ( " " ); } printf ( "\n" ); dummy--; } } // Function to print the pattern of 'L' void printL() { int i, j; for (i = 0; i < height; i++) { printf ( "*" ); for (j = 0; j <= height; j++) { if (i == height - 1) printf ( "*" ); else printf ( " " ); } printf ( "\n" ); } } // Function to print the pattern of 'M' void printM() { int i, j, counter = 0; for (i = 0; i < height; i++) { printf ( "*" ); for (j = 0; j <= height; j++) { if (j == height) printf ( "*" ); else if (j == counter || j == height - counter - 1) printf ( "*" ); else printf ( " " ); } if (counter == height / 2) { counter = -99999; } else counter++; printf ( "\n" ); } } // Function to print the pattern of 'N' void printN() { int i, j, counter = 0; for (i = 0; i < height; i++) { printf ( "*" ); for (j = 0; j <= height; j++) { if (j == height) printf ( "*" ); else if (j == counter) printf ( "*" ); else printf ( " " ); } counter++; printf ( "\n" ); } } // Function to print the pattern of 'O' void printO() { int i, j, space = (height / 3); int 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)) printf ( "*" ); else if ((i == 0 || i == height - 1) && j > abs (space) && j < width - abs (space)) printf ( "*" ); else printf ( " " ); } if (space != 0 && i < height / 2) { space--; } else if (i >= (height / 2 + height / 5)) space--; printf ( "\n" ); } } // Function to print the pattern of 'P' void printP() { int i, j; for (i = 0; i < height; i++) { printf ( "*" ); for (j = 0; j < height; j++) { if ((i == 0 || i == height / 2) && j < height - 1) printf ( "*" ); else if (i < height / 2 && j == height - 1 && i != 0) printf ( "*" ); else printf ( " " ); } printf ( "\n" ); } } // Function to print the pattern of 'Q' void printQ() { printO(); int i, j, d = height; for (i = 0; i < height / 2; i++) { for (j = 0; j <= d; j++) { if (j == d) printf ( "*" ); else printf ( " " ); } printf ( "\n" ); d++; } } // Function to print the pattern of 'R' void printR() { int i, j, half = (height / 2); for (i = 0; i < height; i++) { printf ( "*" ); for (j = 0; j < width; j++) { if ((i == 0 || i == half) && j < (width - 2)) printf ( "*" ); else if (j == (width - 2) && !(i == 0 || i == half)) printf ( "*" ); else printf ( " " ); } printf ( "\n" ); } } // Function to print the pattern of 'S' void printS() { int i, j; for (i = 0; i < height; i++) { for (j = 0; j < height; j++) { if ((i == 0 || i == height / 2 || i == height - 1)) printf ( "*" ); else if (i < height / 2 && j == 0) printf ( "*" ); else if (i > height / 2 && j == height - 1) printf ( "*" ); else printf ( " " ); } printf ( "\n" ); } } // Function to print the pattern of 'T' void printT() { int i, j; for (i = 0; i < height; i++) { for (j = 0; j < height; j++) { if (i == 0) printf ( "*" ); else if (j == height / 2) printf ( "*" ); else printf ( " " ); } printf ( "\n" ); } } // Function to print the pattern of 'U' void printU() { int i, j; for (i = 0; i < height; i++) { if (i != 0 && i != height - 1) printf ( "*" ); else printf ( " " ); for (j = 0; j < height; j++) { if (((i == height - 1) && j >= 0 && j < height - 1)) printf ( "*" ); else if (j == height - 1 && i != 0 && i != height - 1) printf ( "*" ); else printf ( " " ); } printf ( "\n" ); } } // Function to print the pattern of 'V' void printV() { int i, j, counter = 0; for (i = 0; i < height; i++) { for (j = 0; j <= width; j++) { if (j == counter || j == width - counter - 1) printf ( "*" ); else printf ( " " ); } counter++; printf ( "\n" ); } } // Function to print the pattern of 'W' void printW() { int i, j, counter = height / 2; for (i = 0; i < height; i++) { printf ( "*" ); for (j = 0; j <= height; j++) { if (j == height) printf ( "*" ); else if ((i >= height / 2) && (j == counter || j == height - counter - 1)) printf ( "*" ); else printf ( " " ); } if (i >= height / 2) { counter++; } printf ( "\n" ); } } // Function to print the pattern of 'X' void printX() { int i, j, counter = 0; for (i = 0; i <= height; i++) { for (j = 0; j <= height; j++) { if (j == counter || j == height - counter) printf ( "*" ); else printf ( " " ); } counter++; printf ( "\n" ); } } // Function to print the pattern of 'Y' void printY() { 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) printf ( "*" ); else printf ( " " ); } printf ( "\n" ); if (i < height / 2) counter++; } } // Function to print the pattern of 'Z' void printZ() { 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) printf ( "*" ); else printf ( " " ); } counter--; printf ( "\n" ); } } // Function print the pattern of the // alphabets from A to Z void printPattern( char character) { switch (character) { case 'A' : printA(); break ; case 'B' : printB(); break ; case 'C' : printC(); break ; case 'D' : printD(); break ; case 'E' : printE(); break ; case 'F' : printF(); break ; case 'G' : printG(); break ; case 'H' : printH(); break ; case 'I' : printI(); break ; case 'J' : printJ(); break ; case 'K' : printK(); break ; case 'L' : printL(); break ; case 'M' : printM(); break ; case 'N' : printN(); break ; case 'O' : printO(); break ; case 'P' : printP(); break ; case 'Q' : printQ(); break ; case 'R' : printR(); break ; case 'S' : printS(); break ; case 'T' : printT(); break ; case 'U' : printU(); break ; case 'V' : printV(); break ; case 'W' : printW(); break ; case 'X' : printX(); break ; case 'Y' : printY(); break ; case 'Z' : printZ(); break ; } } // Driver Code int main() { char character = 'A' ; printPattern(character); return 0; } |
Java
// Java implementation to print the // pattern of alphabets A to Z using * class GFG { // Below height and width variable can be used // to create a user-defined sized alphabet's pattern // Number of lines for the alphabet's pattern static int height = 5 ; // Number of character width in each line static int width = ( 2 * height) - 1 ; // Function to find the absolute value // of a number D static int abs( int d) { return d < 0 ? - 1 * d : d; } // Function to print the pattern of 'A' static void printA() { 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))) System.out.printf( "*" ); else System.out.printf( " " ); } System.out.printf( "\n" ); n--; } } // Function to print the pattern of 'B' static void printB() { int i, j, half = (height / 2 ); for (i = 0 ; i < height; i++) { System.out.printf( "*" ); for (j = 0 ; j < width; j++) { if ((i == 0 || i == height - 1 || i == half) && j < (width - 2 )) System.out.printf( "*" ); else if (j == (width - 2 ) && !(i == 0 || i == height - 1 || i == half)) System.out.printf( "*" ); else System.out.printf( " " ); } System.out.printf( "\n" ); } } // Function to print the pattern of 'C' static void printC() { int i, j; for (i = 0 ; i < height; i++) { System.out.printf( "*" ); for (j = 0 ; j < (height - 1 ); j++) { if (i == 0 || i == height - 1 ) System.out.printf( "*" ); else continue ; } System.out.printf( "\n" ); } } // Function to print the pattern of 'D' static void printD() { int i, j; for (i = 0 ; i < height; i++) { System.out.printf( "*" ); for (j = 0 ; j < height; j++) { if ((i == 0 || i == height - 1 ) && j < height - 1 ) System.out.printf( "*" ); else if (j == height - 1 && i != 0 && i != height - 1 ) System.out.printf( "*" ); else System.out.printf( " " ); } System.out.printf( "\n" ); } } // Function to print the pattern of 'E' static void printE() { int i, j; for (i = 0 ; i < height; i++) { System.out.printf( "*" ); for (j = 0 ; j < height; j++) { if ((i == 0 || i == height - 1 ) || (i == height / 2 && j <= height / 2 )) System.out.printf( "*" ); else continue ; } System.out.printf( "\n" ); } } // Function to print the pattern of 'F' static void printF() { int i, j; for (i = 0 ; i < height; i++) { System.out.printf( "*" ); for (j = 0 ; j < height; j++) { if ((i == 0 ) || (i == height / 2 && j <= height / 2 )) System.out.printf( "*" ); else continue ; } System.out.printf( "\n" ); } } // Function to print the pattern of 'G' static void printG() { 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 )) System.out.printf( " " ); else if (j == 0 ) System.out.printf( "*" ); else if (i == 0 && j <= height) System.out.printf( "*" ); else if (i == height / 2 && j > height / 2 ) System.out.printf( "*" ); else if (i > height / 2 && j == width - 1 ) System.out.printf( "*" ); else if (i == height - 1 && j < width) System.out.printf( "*" ); else System.out.printf( " " ); } System.out.printf( "\n" ); } } // Function to print the pattern of 'H' static void printH() { int i, j; for (i = 0 ; i < height; i++) { System.out.printf( "*" ); for (j = 0 ; j < height; j++) { if ((j == height - 1 ) || (i == height / 2 )) System.out.printf( "*" ); else System.out.printf( " " ); } System.out.printf( "\n" ); } } // Function to print the pattern of 'I' static void printI() { int i, j; for (i = 0 ; i < height; i++) { for (j = 0 ; j < height; j++) { if (i == 0 || i == height - 1 ) System.out.printf( "*" ); else if (j == height / 2 ) System.out.printf( "*" ); else System.out.printf( " " ); } System.out.printf( "\n" ); } } // Function to print the pattern of 'J' static void printJ() { int i, j; for (i = 0 ; i < height; i++) { for (j = 0 ; j < height; j++) { if (i == height - 1 && (j > 0 && j < height - 1 )) System.out.printf( "*" ); else if ((j == height - 1 && i != height - 1 ) || (i > (height / 2 ) - 1 && j == 0 && i != height - 1 )) System.out.printf( "*" ); else System.out.printf( " " ); } System.out.printf( "\n" ); } } // Function to print the pattern of 'K' static void printK() { int i, j, half = height / 2 , dummy = half; for (i = 0 ; i < height; i++) { System.out.printf( "*" ); for (j = 0 ; j <= half; j++) { if (j == abs(dummy)) System.out.printf( "*" ); else System.out.printf( " " ); } System.out.printf( "\n" ); dummy--; } } // Function to print the pattern of 'L' static void printL() { int i, j; for (i = 0 ; i < height; i++) { System.out.printf( "*" ); for (j = 0 ; j <= height; j++) { if (i == height - 1 ) System.out.printf( "*" ); else System.out.printf( " " ); } System.out.printf( "\n" ); } } // Function to print the pattern of 'M' static void printM() { int i, j, counter = 0 ; for (i = 0 ; i < height; i++) { System.out.printf( "*" ); for (j = 0 ; j <= height; j++) { if (j == height) System.out.printf( "*" ); else if (j == counter || j == height - counter - 1 ) System.out.printf( "*" ); else System.out.printf( " " ); } if (counter == height / 2 ) { counter = - 99999 ; } else counter++; System.out.printf( "\n" ); } } // Function to print the pattern of 'N' static void printN() { int i, j, counter = 0 ; for (i = 0 ; i < height; i++) { System.out.printf( "*" ); for (j = 0 ; j <= height; j++) { if (j == height) System.out.printf( "*" ); else if (j == counter) System.out.printf( "*" ); else System.out.printf( " " ); } counter++; System.out.printf( "\n" ); } } // Function to print the pattern of 'O' static void printO() { int i, j, space = (height / 3 ); int 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)) System.out.printf( "*" ); else if ((i == 0 || i == height - 1 ) && j > abs(space) && j < width - abs(space)) System.out.printf( "*" ); else System.out.printf( " " ); } if (space != 0 && i < height / 2 ) { space--; } else if (i >= (height / 2 + height / 5 )) space--; System.out.printf( "\n" ); } } // Function to print the pattern of 'P' static void printP() { int i, j; for (i = 0 ; i < height; i++) { System.out.printf( "*" ); for (j = 0 ; j < height; j++) { if ((i == 0 || i == height / 2 ) && j < height - 1 ) System.out.printf( "*" ); else if (i < height / 2 && j == height - 1 && i != 0 ) System.out.printf( "*" ); else System.out.printf( " " ); } System.out.printf( "\n" ); } } // Function to print the pattern of 'Q' static void printQ() { printO(); int i, j, d = height; for (i = 0 ; i < height / 2 ; i++) { for (j = 0 ; j <= d; j++) { if (j == d) System.out.printf( "*" ); else System.out.printf( " " ); } System.out.printf( "\n" ); d++; } } // Function to print the pattern of 'R' static void printR() { int i, j, half = (height / 2 ); for (i = 0 ; i < height; i++) { System.out.printf( "*" ); for (j = 0 ; j < width; j++) { if ((i == 0 || i == half) && j < (width - 2 )) System.out.printf( "*" ); else if (j == (width - 2 ) && !(i == 0 || i == half)) System.out.printf( "*" ); else System.out.printf( " " ); } System.out.printf( "\n" ); } } // Function to print the pattern of 'S' static void printS() { int i, j; for (i = 0 ; i < height; i++) { for (j = 0 ; j < height; j++) { if ((i == 0 || i == height / 2 || i == height - 1 )) System.out.printf( "*" ); else if (i < height / 2 && j == 0 ) System.out.printf( "*" ); else if (i > height / 2 && j == height - 1 ) System.out.printf( "*" ); else System.out.printf( " " ); } System.out.printf( "\n" ); } } // Function to print the pattern of 'T' static void printT() { int i, j; for (i = 0 ; i < height; i++) { for (j = 0 ; j < height; j++) { if (i == 0 ) System.out.printf( "*" ); else if (j == height / 2 ) System.out.printf( "*" ); else System.out.printf( " " ); } System.out.printf( "\n" ); } } // Function to print the pattern of 'U' static void printU() { int i, j; for (i = 0 ; i < height; i++) { if (i != 0 && i != height - 1 ) System.out.printf( "*" ); else System.out.printf( " " ); for (j = 0 ; j < height; j++) { if (((i == height - 1 ) && j >= 0 && j < height - 1 )) System.out.printf( "*" ); else if (j == height - 1 && i != 0 && i != height - 1 ) System.out.printf( "*" ); else System.out.printf( " " ); } System.out.printf( "\n" ); } } // Function to print the pattern of 'V' static void printV() { int i, j, counter = 0 ; for (i = 0 ; i < height; i++) { for (j = 0 ; j <= width; j++) { if (j == counter || j == width - counter - 1 ) System.out.printf( "*" ); else System.out.printf( " " ); } counter++; System.out.printf( "\n" ); } } // Function to print the pattern of 'W' static void printW() { int i, j, counter = height / 2 ; for (i = 0 ; i < height; i++) { System.out.printf( "*" ); for (j = 0 ; j <= height; j++) { if (j == height) System.out.printf( "*" ); else if ((i >= height / 2 ) && (j == counter || j == height - counter - 1 )) System.out.printf( "*" ); else System.out.printf( " " ); } if (i >= height / 2 ) { counter++; } System.out.printf( "\n" ); } } // Function to print the pattern of 'X' static void printX() { int i, j, counter = 0 ; for (i = 0 ; i <= height; i++) { for (j = 0 ; j <= height; j++) { if (j == counter || j == height - counter) System.out.printf( "*" ); else System.out.printf( " " ); } counter++; System.out.printf( "\n" ); } } // Function to print the pattern of 'Y' static void printY() { 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 ) System.out.printf( "*" ); else System.out.printf( " " ); } System.out.printf( "\n" ); if (i < height / 2 ) counter++; } } // Function to print the pattern of 'Z' static void printZ() { 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) System.out.printf( "*" ); else System.out.printf( " " ); } counter--; System.out.printf( "\n" ); } } // Function print the pattern of the // alphabets from A to Z static void printPattern( char character) { switch (character) { case 'A' : printA(); break ; case 'B' : printB(); break ; case 'C' : printC(); break ; case 'D' : printD(); break ; case 'E' : printE(); break ; case 'F' : printF(); break ; case 'G' : printG(); break ; case 'H' : printH(); break ; case 'I' : printI(); break ; case 'J' : printJ(); break ; case 'K' : printK(); break ; case 'L' : printL(); break ; case 'M' : printM(); break ; case 'N' : printN(); break ; case 'O' : printO(); break ; case 'P' : printP(); break ; case 'Q' : printQ(); break ; case 'R' : printR(); break ; case 'S' : printS(); break ; case 'T' : printT(); break ; case 'U' : printU(); break ; case 'V' : printV(); break ; case 'W' : printW(); break ; case 'X' : printX(); break ; case 'Y' : printY(); break ; case 'Z' : printZ(); break ; } } // Driver Code public static void main(String[] args) { char character = 'A' ; printPattern(character); } } // This code is contributed by PrinciRaj1992 |
Python3
# Python implementation to print the # pattern of alphabets A to Z using * # Below height and width variable can be used # to create a user-defined sized alphabet's pattern # Number of lines for the alphabet's pattern height = 5 # Number of character width in each line width = ( 2 * height) - 1 # Function to find the absolute value # of a number D def abs (d): if d < 0 : return - 1 * d else : return d # Function to print the pattern of 'A' def printA(): n = width / / 2 for i in range ( 0 , height): for j in range ( 0 , width + 1 ): if (j = = n or j = = (width - n) or (i = = (height / / 2 ) and j > n and j < (width - n))): print ( "*" , end = "") else : print (end = " " ) print () n = n - 1 # Function to print the pattern of 'B' def printB() : half = height / / 2 for i in range ( 0 ,height) : print ( "*" ,end = "") for j in range ( 0 ,width) : if ((i = = 0 or i = = height - 1 or i = = half) and j < (width - 2 )) : print ( "*" ,end = "") elif (j = = (width - 2 ) and not (i = = 0 or i = = height - 1 or i = = half)) : print ( "*" ,end = "") else : print (end = " " ) print () # Function to print the pattern of 'C' def printC() : for i in range ( 0 ,height) : print ( "*" ,end = "") for j in range ( 0 ,height - 1 ) : if (i = = 0 or i = = height - 1 ) : print ( "*" ,end = "") else : continue print () # Function to print the pattern of 'D' def printD() : for i in range ( 0 ,height) : print ( "*" ,end = "") for j in range ( 0 ,height) : if ( (i = = 0 or i = = height - 1 ) and j < height - 1 ): print ( "*" ,end = "") elif (j = = height - 1 and i ! = 0 and i ! = height - 1 ) : print ( "*" ,end = "") else : print (end = " " ) print () # Function to print the pattern of 'E' def printE() : for i in range ( 0 ,height) : print ( "*" ,end = "") for j in range ( 0 ,height) : if ( (i = = 0 or i = = height - 1 ) or (i = = height / / 2 and j < = height / / 2 ) ): print ( "*" ,end = "") else : continue print () # Function to print the pattern of 'F' def printF() : for i in range ( 0 ,height) : print ( "*" ,end = "") for j in range ( 0 ,height) : if ( (i = = 0 ) or (i = = height / / 2 and j < = height / / 2 ) ): print ( "*" ,end = "") else : continue print () # Function to print the pattern of 'G' def printG() : for i in range ( 0 ,height) : for j in range ( 0 ,width - 1 ) : if ((i = = 0 or i = = height - 1 ) and (j = = 0 or j = = width - 2 )) : print (end = " " ) elif (j = = 0 ) : print ( "*" ,end = "") elif (i = = 0 and j < = height) : print ( "*" ,end = "") elif (i = = height / / 2 and j > height / / 2 ) : print ( "*" ,end = "") elif (i > height / / 2 and j = = width - 2 ) : print ( "*" ,end = "") elif (i = = height - 1 and j < width - 1 ) : print ( "*" ,end = "") else : print (end = " " ) print () # Function to print the pattern of 'H' def printH() : for i in range ( 0 ,height) : print ( "*" ,end = "") for j in range ( 0 ,height) : if ( (j = = height - 1 ) or (i = = height / / 2 ) ): print ( "*" ,end = "") else : print (end = " " ) print () # Function to print the pattern of 'I' def printI() : for i in range ( 0 ,height) : for j in range ( 0 ,height) : if ( i = = 0 or i = = height - 1 ): print ( "*" ,end = "") elif ( j = = height / / 2 ) : print ( "*" ,end = "") else : print (end = " " ) print () # Function to print the pattern of 'J' def printJ() : for i in range ( 0 ,height) : for j in range ( 0 ,height) : if ( i = = height - 1 and (j > 0 and j < height - 1 ) ): print ( "*" ,end = "") elif ( (j = = height - 1 and i ! = height - 1 ) or (i > (height / / 2 ) - 1 and j = = 0 and i ! = height - 1 ) ) : print ( "*" ,end = "") else : print (end = " " ) print () # Function to print the pattern of 'K' def printK() : half = height / / 2 dummy = half for i in range ( 0 ,height) : print ( "*" ,end = "") for j in range ( 0 ,half + 1 ) : if ( j = = abs (dummy) ): print ( "*" ,end = "") else : print (end = " " ) print () dummy = dummy - 1 # Function to print the pattern of 'L' def printL() : for i in range ( 0 ,height) : print ( "*" ,end = "") for j in range ( 0 ,height + 1 ) : if ( i = = height - 1 ): print ( "*" ,end = "") else : print (end = " " ) print () # Function to print the pattern of 'M' def printM() : counter = 0 for i in range ( 0 ,height) : print ( "*" ,end = "") for j in range ( 0 ,height + 1 ) : if ( j = = height ): print ( "*" ,end = "") elif ( j = = counter or j = = height - counter - 1 ) : print ( "*" ,end = "") else : print (end = " " ) if (counter = = height / / 2 ) : counter = - 99999 else : counter = counter + 1 print () # Function to print the pattern of 'N' def printN() : counter = 0 for i in range ( 0 ,height) : print ( "*" ,end = "") for j in range ( 0 ,height + 1 ) : if ( j = = height ): print ( "*" ,end = "") elif ( j = = counter) : print ( "*" ,end = "") else : print (end = " " ) counter = counter + 1 print () # Function to print the pattern of 'O' def printO() : space = height / / 3 width = height / / 2 + height / / 5 + space + space for i in range ( 0 ,height) : for j in range ( 0 ,width + 1 ) : if ( j = = width - abs (space) or j = = abs (space)): print ( "*" ,end = "") elif ( (i = = 0 or i = = height - 1 ) and j > abs (space) and j < width - abs (space) ) : print ( "*" ,end = "") else : print (end = " " ) if ( space ! = 0 and i < height / / 2 ) : space = space - 1 elif ( i > = (height / / 2 + height / / 5 ) ) : space = space - 1 print () # Function to print the pattern of 'P' def printP() : for i in range ( 0 ,height) : print ( "*" ,end = "") for j in range ( 0 ,height) : if ( (i = = 0 or i = = height / / 2 ) and j < height - 1 ): print ( "*" ,end = "") elif ( i < height / / 2 and j = = height - 1 and i ! = 0 ) : print ( "*" ,end = "") else : print (end = " " ) print () # Function to print the pattern of 'Q' def printQ() : printO() d = height for i in range ( 0 ,height / / 2 ) : for j in range ( 0 ,d + 1 ) : if ( j = = d ): print ( "*" ,end = "") else : print (end = " " ) print () d = d + 1 # Function to print the pattern of 'R' def printR() : half = (height / / 2 ) for i in range ( 0 ,height) : print ( "*" ,end = "") for j in range ( 0 ,width) : if ( (i = = 0 or i = = half) and j < (width - 2 ) ): print ( "*" ,end = "") elif ( j = = (width - 2 ) and not (i = = 0 or i = = half) ) : print ( "*" ,end = "") else : print (end = " " ) print () # Function to print the pattern of 'S' def printS() : for i in range ( 0 ,height) : for j in range ( 0 ,height) : if ( (i = = 0 or i = = height / / 2 or i = = height - 1 ) ): print ( "*" ,end = "") elif ( i < height / / 2 and j = = 0 ) : print ( "*" ,end = "") elif ( i > height / / 2 and j = = height - 1 ) : print ( "*" ,end = "") else : print (end = " " ) print () # Function to print the pattern of 'T' def printT() : for i in range ( 0 ,height) : for j in range ( 0 ,height) : if ( i = = 0 ): print ( "*" ,end = "") elif ( j = = height / / 2 ) : print ( "*" ,end = "") else : print (end = " " ) print () # Function to print the pattern of 'U' def printU() : for i in range ( 0 ,height) : if (i ! = 0 and i ! = height - 1 ) : print ( "*" ,end = "") else : print (end = " " ) for j in range ( 0 ,height) : if ( ((i = = height - 1 ) and j > = 0 and j < height - 1 ) ): print ( "*" ,end = "") elif ( j = = height - 1 and i ! = 0 and i ! = height - 1 ) : print ( "*" ,end = "") else : print (end = " " ) print () # Function to print the pattern of 'V' def printV() : counter = 0 for i in range ( 0 ,height) : for j in range ( 0 ,width + 1 ) : if ( j = = counter or j = = width - counter - 1 ): print ( "*" ,end = "") else : print (end = " " ) counter = counter + 1 print () # Function to print the pattern of 'W' def printW() : counter = height / / 2 for i in range ( 0 ,height) : print ( "*" ,end = "") for j in range ( 0 ,height + 1 ) : if ( j = = height ): print ( "*" ,end = "") elif ( (i > = height / / 2 ) and (j = = counter or j = = height - counter - 1 ) ) : print ( "*" ,end = "") else : print (end = " " ) if ( i > = height / / 2 ) : counter = counter + 1 print () # Function to print the pattern of 'X' def printX() : counter = 0 for i in range ( 0 ,height + 1 ) : for j in range ( 0 ,height + 1 ) : if ( j = = counter or j = = height - counter ): print ( "*" ,end = "") else : print (end = " " ) counter = counter + 1 print () # Function to print the pattern of 'Y' def printY() : counter = 0 for i in range ( 0 ,height) : for j in range ( 0 ,height + 1 ) : if ( j = = counter or j = = height - counter and i < = height / / 2 ): print ( "*" ,end = "") else : print (end = " " ) print () if (i < height / / 2 ) : counter = counter + 1 # Function to print the pattern of 'Z' def printZ() : counter = height - 1 for i in range ( 0 ,height) : for j in range ( 0 ,height) : if ( i = = 0 or i = = height - 1 or j = = counter ): print ( "*" ,end = "") else : print (end = " " ) counter = counter - 1 print () # Function print the pattern of the # alphabets from A to Z def printPattern(character) : if character = = 'A' : return printA() elif character = = 'B' : return printB() elif character = = 'C' : return printC() elif character = = 'D' : return printD() elif character = = 'E' : return printE(), elif character = = 'F' : return printF(), elif character = = 'G' : return printG(), elif character = = 'H' : return printH(), elif character = = 'I' : return printI(), elif character = = 'J' : return printJ(), elif character = = 'K' : return printK(), elif character = = 'L' : return printL(), elif character = = 'M' : return printM(), elif character = = 'N' : return printN(), elif character = = 'O' : return printO(), elif character = = 'P' : return printP(), elif character = = 'Q' : return printQ(), elif character = = 'R' : return printR(), elif character = = 'S' : return printS(), elif character = = 'T' : return printT(), elif character = = 'U' : return printU(), elif character = = 'V' : return printV(), elif character = = 'W' : return printW(), elif character = = 'X' : return printX(), elif character = = 'Y' : return printY() else : printZ() # Driver Code if __name__ = = "__main__" : character = 'A' printPattern(character) # This code is contributed by rakeshsahni |
C#
// C# implementation to print the // pattern of alphabets A to Z using * using System; class GFG { // Below height and width variable can be used // to create a user-defined sized alphabet's pattern // Number of lines for the alphabet's pattern static int height = 5; // Number of character width in each line static int width = (2 * height) - 1; // Function to find the absolute value // of a number D static int abs( int d) { return d < 0 ? -1 * d : d; } // Function to print the pattern of 'A' static void printA() { 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))) Console.Write( "*" ); else Console.Write( " " ); } Console.Write( "\n" ); n--; } } // Function to print the pattern of 'B' static void printB() { int i, j, half = (height / 2); for (i = 0; i < height; i++) { Console.Write( "*" ); for (j = 0; j < width; j++) { if ((i == 0 || i == height - 1 || i == half) && j < (width - 2)) Console.Write( "*" ); else if (j == (width - 2) && !(i == 0 || i == height - 1 || i == half)) Console.Write( "*" ); else Console.Write( " " ); } Console.Write( "\n" ); } } // Function to print the pattern of 'C' static void printC() { int i, j; for (i = 0; i < height; i++) { Console.Write( "*" ); for (j = 0; j < (height - 1); j++) { if (i == 0 || i == height - 1) Console.Write( "*" ); else continue ; } Console.Write( "\n" ); } } // Function to print the pattern of 'D' static void printD() { int i, j; for (i = 0; i < height; i++) { Console.Write( "*" ); for (j = 0; j < height; j++) { if ((i == 0 || i == height - 1) && j < height - 1) Console.Write( "*" ); else if (j == height - 1 && i != 0 && i != height - 1) Console.Write( "*" ); else Console.Write( " " ); } Console.Write( "\n" ); } } // Function to print the pattern of 'E' static void printE() { int i, j; for (i = 0; i < height; i++) { Console.Write( "*" ); for (j = 0; j < height; j++) { if ((i == 0 || i == height - 1) || (i == height / 2 && j <= height / 2)) Console.Write( "*" ); else continue ; } Console.Write( "\n" ); } } // Function to print the pattern of 'F' static void printF() { int i, j; for (i = 0; i < height; i++) { Console.Write( "*" ); for (j = 0; j < height; j++) { if ((i == 0) || (i == height / 2 && j <= height / 2)) Console.Write( "*" ); else continue ; } Console.Write( "\n" ); } } // Function to print the pattern of 'G' static void printG() { 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)) Console.Write( " " ); else if (j == 0) Console.Write( "*" ); else if (i == 0 && j <= height) Console.Write( "*" ); else if (i == height / 2 && j > height / 2) Console.Write( "*" ); else if (i > height / 2 && j == width - 1) Console.Write( "*" ); else if (i == height - 1 && j < width) Console.Write( "*" ); else Console.Write( " " ); } Console.Write( "\n" ); } } // Function to print the pattern of 'H' static void printH() { int i, j; for (i = 0; i < height; i++) { Console.Write( "*" ); for (j = 0; j < height; j++) { if ((j == height - 1) || (i == height / 2)) Console.Write( "*" ); else Console.Write( " " ); } Console.Write( "\n" ); } } // Function to print the pattern of 'I' static void printI() { int i, j; for (i = 0; i < height; i++) { for (j = 0; j < height; j++) { if (i == 0 || i == height - 1) Console.Write( "*" ); else if (j == height / 2) Console.Write( "*" ); else Console.Write( " " ); } Console.Write( "\n" ); } } // Function to print the pattern of 'J' static void printJ() { int i, j; for (i = 0; i < height; i++) { for (j = 0; j < height; j++) { if (i == height - 1 && (j > 0 && j < height - 1)) Console.Write( "*" ); else if ((j == height - 1 && i != height - 1) || (i > (height / 2) - 1 && j == 0 && i != height - 1)) Console.Write( "*" ); else Console.Write( " " ); } Console.Write( "\n" ); } } // Function to print the pattern of 'K' static void printK() { int i, j, half = height / 2, dummy = half; for (i = 0; i < height; i++) { Console.Write( "*" ); for (j = 0; j <= half; j++) { if (j == abs(dummy)) Console.Write( "*" ); else Console.Write( " " ); } Console.Write( "\n" ); dummy--; } } // Function to print the pattern of 'L' static void printL() { int i, j; for (i = 0; i < height; i++) { Console.Write( "*" ); for (j = 0; j <= height; j++) { if (i == height - 1) Console.Write( "*" ); else Console.Write( " " ); } Console.Write( "\n" ); } } // Function to print the pattern of 'M' static void printM() { int i, j, counter = 0; for (i = 0; i < height; i++) { Console.Write( "*" ); for (j = 0; j <= height; j++) { if (j == height) Console.Write( "*" ); else if (j == counter || j == height - counter - 1) Console.Write( "*" ); else Console.Write( " " ); } if (counter == height / 2) { counter = -99999; } else counter++; Console.Write( "\n" ); } } // Function to print the pattern of 'N' static void printN() { int i, j, counter = 0; for (i = 0; i < height; i++) { Console.Write( "*" ); for (j = 0; j <= height; j++) { if (j == height) Console.Write( "*" ); else if (j == counter) Console.Write( "*" ); else Console.Write( " " ); } counter++; Console.Write( "\n" ); } } // Function to print the pattern of 'O' static void printO() { int i, j, space = (height / 3); int 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)) Console.Write( "*" ); else if ((i == 0 || i == height - 1) && j > abs(space) && j < width - abs(space)) Console.Write( "*" ); else Console.Write( " " ); } if (space != 0 && i < height / 2) { space--; } else if (i >= (height / 2 + height / 5)) space--; Console.Write( "\n" ); } } // Function to print the pattern of 'P' static void printP() { int i, j; for (i = 0; i < height; i++) { Console.Write( "*" ); for (j = 0; j < height; j++) { if ((i == 0 || i == height / 2) && j < height - 1) Console.Write( "*" ); else if (i < height / 2 && j == height - 1 && i != 0) Console.Write( "*" ); else Console.Write( " " ); } Console.Write( "\n" ); } } // Function to print the pattern of 'Q' static void printQ() { printO(); int i, j, d = height; for (i = 0; i < height / 2; i++) { for (j = 0; j <= d; j++) { if (j == d) Console.Write( "*" ); else Console.Write( " " ); } Console.Write( "\n" ); d++; } } // Function to print the pattern of 'R' static void printR() { int i, j, half = (height / 2); for (i = 0; i < height; i++) { Console.Write( "*" ); for (j = 0; j < width; j++) { if ((i == 0 || i == half) && j < (width - 2)) Console.Write( "*" ); else if (j == (width - 2) && !(i == 0 || i == half)) Console.Write( "*" ); else Console.Write( " " ); } Console.Write( "\n" ); } } // Function to print the pattern of 'S' static void printS() { int i, j; for (i = 0; i < height; i++) { for (j = 0; j < height; j++) { if ((i == 0 || i == height / 2 || i == height - 1)) Console.Write( "*" ); else if (i < height / 2 && j == 0) Console.Write( "*" ); else if (i > height / 2 && j == height - 1) Console.Write( "*" ); else Console.Write( " " ); } Console.Write( "\n" ); } } // Function to print the pattern of 'T' static void printT() { int i, j; for (i = 0; i < height; i++) { for (j = 0; j < height; j++) { if (i == 0) Console.Write( "*" ); else if (j == height / 2) Console.Write( "*" ); else Console.Write( " " ); } Console.Write( "\n" ); } } // Function to print the pattern of 'U' static void printU() { int i, j; for (i = 0; i < height; i++) { if (i != 0 && i != height - 1) Console.Write( "*" ); else Console.Write( " " ); for (j = 0; j < height; j++) { if (((i == height - 1) && j >= 0 && j < height - 1)) Console.Write( "*" ); else if (j == height - 1 && i != 0 && i != height - 1) Console.Write( "*" ); else Console.Write( " " ); } Console.Write( "\n" ); } } // Function to print the pattern of 'V' static void printV() { int i, j, counter = 0; for (i = 0; i < height; i++) { for (j = 0; j <= width; j++) { if (j == counter || j == width - counter - 1) Console.Write( "*" ); else Console.Write( " " ); } counter++; Console.Write( "\n" ); } } // Function to print the pattern of 'W' static void printW() { int i, j, counter = height / 2; for (i = 0; i < height; i++) { Console.Write( "*" ); for (j = 0; j <= height; j++) { if (j == height) Console.Write( "*" ); else if ((i >= height / 2) && (j == counter || j == height - counter - 1)) Console.Write( "*" ); else Console.Write( " " ); } if (i >= height / 2) { counter++; } Console.Write( "\n" ); } } // Function to print the pattern of 'X' static void printX() { int i, j, counter = 0; for (i = 0; i <= height; i++) { for (j = 0; j <= height; j++) { if (j == counter || j == height - counter) Console.Write( "*" ); else Console.Write( " " ); } counter++; Console.Write( "\n" ); } } // Function to print the pattern of 'Y' static void printY() { 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) Console.Write( "*" ); else Console.Write( " " ); } Console.Write( "\n" ); if (i < height / 2) counter++; } } // Function to print the pattern of 'Z' static void printZ() { 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) Console.Write( "*" ); else Console.Write( " " ); } counter--; Console.Write( "\n" ); } } // Function print the pattern of the // alphabets from A to Z static void printPattern( char character) { switch (character) { case 'A' : printA(); break ; case 'B' : printB(); break ; case 'C' : printC(); break ; case 'D' : printD(); break ; case 'E' : printE(); break ; case 'F' : printF(); break ; case 'G' : printG(); break ; case 'H' : printH(); break ; case 'I' : printI(); break ; case 'J' : printJ(); break ; case 'K' : printK(); break ; case 'L' : printL(); break ; case 'M' : printM(); break ; case 'N' : printN(); break ; case 'O' : printO(); break ; case 'P' : printP(); break ; case 'Q' : printQ(); break ; case 'R' : printR(); break ; case 'S' : printS(); break ; case 'T' : printT(); break ; case 'U' : printU(); break ; case 'V' : printV(); break ; case 'W' : printW(); break ; case 'X' : printX(); break ; case 'Y' : printY(); break ; case 'Z' : printZ(); break ; } } // Driver Code public static void Main(String[] args) { char character = 'A' ; printPattern(character); } } // This code is contributed by PrinciRaj1992 |
Javascript
<script> // JavaScript implementation to print the // pattern of alphabets A to Z using * // Below height and width variable can be used // to create a user-defined sized alphabet's pattern // Number of lines for the alphabet's pattern let height = 5; // Number of character width in each line let width = (2 * height) - 1; // Function to find the absolute value // of a number D const abs = (d) => { return d < 0 ? -1 * d : d; } // Function to print the pattern of 'A' const printA = () => { let n = parseInt(width / 2), i, j; for (i = 0; i < height; i++) { for (j = 0; j <= width; j++) { if (j == n || j == (width - n) || (i == parseInt(height / 2) && j > n && j < (width - n))) document.write( "*" ); else document.write( " " ); } document.write(`<br/>`); n--; } } // Function to print the pattern of 'B' const printB = () => { let i, j, half = parseInt(height / 2); for (i = 0; i < height; i++) { document.write( "*" ); for (j = 0; j < width; j++) { if ((i == 0 || i == height - 1 || i == half) && j < (width - 2)) document.write( "*" ); else if (j == (width - 2) && !(i == 0 || i == height - 1 || i == half)) document.write( "*" ); else document.write( " " ); } document.write(`<br/>`); } } // Function to print the pattern of 'C' const printC = () => { let i, j; for (i = 0; i < height; i++) { document.write( "*" ); for (j = 0; j < (height - 1); j++) { if (i == 0 || i == height - 1) document.write( "*" ); else continue ; } document.write(`<br/>`); } } // Function to print the pattern of 'D' const printD = () => { let i, j; for (i = 0; i < height; i++) { document.write( "*" ); for (j = 0; j < height; j++) { if ((i == 0 || i == height - 1) && j < height - 1) document.write( "*" ); else if (j == height - 1 && i != 0 && i != height - 1) document.write( "*" ); else document.write( " " ); } document.write(`<br/>`); } } // Function to print the pattern of 'E' const printE = () => { let i, j; for (i = 0; i < height; i++) { document.write( "*" ); for (j = 0; j < height; j++) { if ((i == 0 || i == height - 1) || (i == parseInt(height / 2) && j <= parseInt(height / 2))) document.write( "*" ); else continue ; } document.write(`<br/>`); } } // Function to print the pattern of 'F' const printF = () => { let i, j; for (i = 0; i < height; i++) { document.write( "*" ); for (j = 0; j < height; j++) { if ((i == 0) || (i == parseInt(height / 2) && j <= parseInt(height / 2))) document.write( "*" ); else continue ; } document.write(`<br/>`); } } // Function to print the pattern of 'G' const printG = () => { let 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)) document.write( " " ); else if (j == 0) document.write( "*" ); else if (i == 0 && j <= height) document.write( "*" ); else if (i == parseInt(height / 2) && j > parseInt(height / 2)) document.write( "*" ); else if (i > parseInt(height / 2) && j == width - 1) document.write( "*" ); else if (i == height - 1 && j < width) document.write( "*" ); else document.write( " " ); } document.write(`<br/>`); } } // Function to print the pattern of 'H' const printH = () => { let i, j; for (i = 0; i < height; i++) { document.write( "*" ); for (j = 0; j < height; j++) { if ((j == height - 1) || (i == parseInt(height / 2))) document.write( "*" ); else document.write( " " ); } document.write(`<br/>`); } } // Function to print the pattern of 'I' const printI = () => { let i, j; for (i = 0; i < height; i++) { for (j = 0; j < height; j++) { if (i == 0 || i == height - 1) document.write( "*" ); else if (j == parseInt(height / 2)) document.write( "*" ); else document.write( " " ); } document.write(`<br/>`); } } // Function to print the pattern of 'J' const printJ = () => { let i, j; for (i = 0; i < height; i++) { for (j = 0; j < height; j++) { if (i == height - 1 && (j > 0 && j < height - 1)) document.write( "*" ); else if ((j == height - 1 && i != height - 1) || (i > parseInt(height / 2) - 1 && j == 0 && i != height - 1)) document.write( "*" ); else document.write( " " ); } document.write(`<br/>`); } } // Function to print the pattern of 'K' const printK = () => { let i, j, half = parseInt(height / 2), dummy = half; for (i = 0; i < height; i++) { document.write( "*" ); for (j = 0; j <= half; j++) { if (j == abs(dummy)) document.write( "*" ); else document.write( " " ); } document.write(`<br/>`); dummy--; } } // Function to print the pattern of 'L' const printL = () => { let i, j; for (i = 0; i < height; i++) { document.write( "*" ); for (j = 0; j <= height; j++) { if (i == height - 1) document.write( "*" ); else document.write( " " ); } document.write(`<br/>`); } } // Function to print the pattern of 'M' const printM = () => { let i, j, counter = 0; for (i = 0; i < height; i++) { document.write( "*" ); for (j = 0; j <= height; j++) { if (j == height) document.write( "*" ); else if (j == counter || j == height - counter - 1) document.write( "*" ); else document.write( " " ); } if (counter == parseInt(height / 2)) { counter = -99999; } else counter++; document.write(`<br/>`); } } // Function to print the pattern of 'N' const printN = () => { let i, j, counter = 0; for (i = 0; i < height; i++) { document.write( "*" ); for (j = 0; j <= height; j++) { if (j == height) document.write( "*" ); else if (j == counter) document.write( "*" ); else document.write( " " ); } counter++; document.write(`<br/>`); } } // Function to print the pattern of 'O' const printO = () => { let i, j, space = parseInt(height / 3); let width = parseInt(height / 2) + parseInt(height / 5) + space + space; for (i = 0; i < height; i++) { for (j = 0; j <= width; j++) { if (j == width - abs(space) || j == abs(space)) document.write( "*" ); else if ((i == 0 || i == height - 1) && j > abs(space) && j < width - abs(space)) document.write( "*" ); else document.write( " " ); } if (space != 0 && i < parseInt(height / 2)) { space--; } else if (i >= (parseInt(height / 2) + parseInt(height / 5))) space--; document.write(`<br/>`); } } // Function to print the pattern of 'P' const printP = () => { let i, j; for (i = 0; i < height; i++) { document.write( "*" ); for (j = 0; j < height; j++) { if ((i == 0 || i == parseInt(height / 2)) && j < height - 1) document.write( "*" ); else if (i < parseInt(height / 2) && j == height - 1 && i != 0) document.write( "*" ); else document.write( " " ); } document.write(`<br/>`); } } // Function to print the pattern of 'Q' const printQ = () => { printO(); let i, j, d = height; for (i = 0; i < parseInt(height / 2); i++) { for (j = 0; j <= d; j++) { if (j == d) document.write( "*" ); else document.write( " " ); } document.write(`<br/>`); d++; } } // Function to print the pattern of 'R' const printR = () => { let i, j, half = parseInt(height / 2); for (i = 0; i < height; i++) { document.write( "*" ); for (j = 0; j < width; j++) { if ((i == 0 || i == half) && j < (width - 2)) document.write( "*" ); else if (j == (width - 2) && !(i == 0 || i == half)) document.write( "*" ); else document.write( " " ); } document.write(`<br/>`); } } // Function to print the pattern of 'S' const printS = () => { let i, j; for (i = 0; i < height; i++) { for (j = 0; j < height; j++) { if ((i == 0 || i == parseInt(height / 2) || i == height - 1)) document.write( "*" ); else if (i < parseInt(height / 2) && j == 0) document.write( "*" ); else if (i > parseInt(height / 2) && j == height - 1) document.write( "*" ); else document.write( " " ); } document.write(`<br/>`); } } // Function to print the pattern of 'T' const printT = () => { let i, j; for (i = 0; i < height; i++) { for (j = 0; j < height; j++) { if (i == 0) document.write( "*" ); else if (j == parseInt(height / 2)) document.write( "*" ); else document.write( " " ); } document.write(`<br/>`); } } // Function to print the pattern of 'U' const printU = () => { let i, j; for (i = 0; i < height; i++) { if (i != 0 && i != height - 1) document.write( "*" ); else document.write( " " ); for (j = 0; j < height; j++) { if (((i == height - 1) && j >= 0 && j < height - 1)) document.write( "*" ); else if (j == height - 1 && i != 0 && i != height - 1) document.write( "*" ); else document.write( " " ); } document.write(`<br/>`); } } // Function to print the pattern of 'V' const printV = () => { let i, j, counter = 0; for (i = 0; i < height; i++) { for (j = 0; j <= width; j++) { if (j == counter || j == width - counter - 1) document.write( "*" ); else document.write( " " ); } counter++; document.write(`<br/>`); } } // Function to print the pattern of 'W' const printW = () => { let i, j, counter = parseInt(height / 2); for (i = 0; i < height; i++) { document.write( "*" ); for (j = 0; j <= height; j++) { if (j == height) document.write( "*" ); else if ((i >= parseInt(height / 2)) && (j == counter || j == height - counter - 1)) document.write( "*" ); else document.write( " " ); } if (i >= parseInt(height / 2)) { counter++; } document.write(`<br/>`); } } // Function to print the pattern of 'X' const printX = () => { let i, j, counter = 0; for (i = 0; i <= height; i++) { for (j = 0; j <= height; j++) { if (j == counter || j == height - counter) document.write( "*" ); else document.write( " " ); } counter++; document.write(`<br/>`); } } // Function to print the pattern of 'Y' const printY = () => { let i, j, counter = 0; for (i = 0; i < height; i++) { for (j = 0; j <= height; j++) { if (j == counter || j == height - counter && i <= parseInt(height / 2)) document.write( "*" ); else document.write( " " ); } document.write(`<br/>`); if (i < parseInt(height / 2)) counter++; } } // Function to print the pattern of 'Z' const printZ = () => { let 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) document.write( "*" ); else document.write( " " ); } counter--; document.write(`<br/>`); } } // Function print the pattern of the // alphabets from A to Z const printPattern = (character) => { switch (character) { case 'A' : printA(); break ; case 'B' : printB(); break ; case 'C' : printC(); break ; case 'D' : printD(); break ; case 'E' : printE(); break ; case 'F' : printF(); break ; case 'G' : printG(); break ; case 'H' : printH(); break ; case 'I' : printI(); break ; case 'J' : printJ(); break ; case 'K' : printK(); break ; case 'L' : printL(); break ; case 'M' : printM(); break ; case 'N' : printN(); break ; case 'O' : printO(); break ; case 'P' : printP(); break ; case 'Q' : printQ(); break ; case 'R' : printR(); break ; case 'S' : printS(); break ; case 'T' : printT(); break ; case 'U' : printU(); break ; case 'V' : printV(); break ; case 'W' : printW(); break ; case 'X' : printX(); break ; case 'Y' : printY(); break ; case 'Z' : printZ(); break ; } } // Driver Code let character = 'A' ; printPattern(character); // This code is contributed by rakeshsahni </script> |
Output:
** * * ****** * * * *
Time complexity: O(h^2) where h is height of the alphabet
Auxiliary space: O(1)