Program to Print Crown Character Pattern
In the previous article, we have discussed Java Program to Print Hollow Square Inside a Square Character Pattern
In this article we are going to see how to print crown character pattern.
- Java Code to Print Crown Character Pattern
- C Code to Print Crown Character Pattern
- C++ Code to Print Crown Character Pattern
For example: When rows: 10 A J T A J T AB IJK ST ABC HIJKL RST ABCD GHIJKLM QRST ABCDEFGHIJKLMN PQRST ABCDEFGHIJKLMNOPQRST ABCDEFGHIJKLMNOPQRST ABCDEFGHIJKLMNOPQRST
Now, let’s see the actual program to print it.
Don’t stop learning now. Get hold of all the important Java fundamentals with the Simple java program example guide and practice well.
Approach:
- Enter total row and store it in an integer variable
row
. - Take height as h and
h=(row -1)/2
. - Take first for loop to print the row value and character for each row .
- Take inner for loops to print column value i.e., character else print the spaces according to the conditions.
- Then go on printing the characters according to loop.
Java Code to Print Crown Character Pattern
import java.util.*; public class Main { public static void main(String args[]) { // taking variable for loop iteration and row . int row,r,c,h; //ASCII value taken 65, ASCII value of A int asciiAlpha = 65; //creating scanner class object Scanner s = new Scanner(System.in); // entering the number of row System.out.print("Enter rows : "); row = s.nextInt(); //calculating height of crown h= (row-1)/2; //outer loop // iterating all thge rows(covering height) for (r = 0; r < h ; r++) { //inner loop // printing characterss of the crown for (c = 0; c < row; c++) { //printing the characters for top part of crown //means only in first row characters are printed if (r == 0) { if (c == 0 || c == h || c == row - 1) System.out.print((char)(c + asciiAlpha)); else System.out.print(" "); } //last row value printed else if (r == h- 1) System.out.print((char)(c + asciiAlpha)); //remaining values in mid part printed else if ((c < r || c > h - r) &&(c < h + r || c >= row - r)) System.out.print((char)(c + asciiAlpha)); else System.out.print(" "); } // taking to the new line System.out.println(); } } }
Output : Enter rows: 10 A J T A J T AB IJK ST ABC HIJKL RST ABCD GHIJKLM QRST ABCDEFGHIJKLMN PQRST ABCDEFGHIJKLMNOPQRST ABCDEFGHIJKLMNOPQRST ABCDEFGHIJKLMNOPQRST
C Code to Print Crown Character Pattern
#include <stdio.h> int main() { int r, row, h,c ; int asciiAlpha = 65; printf("Enter rows: "); scanf("%d", &row); h= (row-1)/2; for (r = 0; r < h ; r++) { for (c = 0; c < row; c++) { // for first row, print '*' i.e, for top part of crown if (r == 0) { if (c == 0 || c == h || c == row - 1) printf("%c",(c + asciiAlpha)); else printf(" "); } else if (r == h- 1) printf("%c",(c + asciiAlpha)); else if ((c < r || c > h - r) &&(c < h + r || c >= row - r)) printf("%c",(c + asciiAlpha)); else printf(" "); } printf("\n"); } return 0; }
Output: Enter rows: 20 A J T A J T AB IJK ST ABC HIJKL RST ABCD GHIJKLM QRST ABCDEFGHIJKLMN PQRST ABCDEFGHIJKLMNOPQRST ABCDEFGHIJKLMNOPQRST ABCDEFGHIJKLMNOPQRST
C++ Code to Print Crown Character Pattern
#include <iostream> using namespace std; int main() { int row, r , c ,h; int asciiAlpha = 65; cout << "Enter rows: "; cin >> row; h= (row-1)/2; for (r = 0; r < h ; r++) { for (c = 0; c < row; c++) { if (r == 0) { if (c == 0 || c == h || c == row - 1) cout << (char)(c + asciiAlpha); else cout << " "; } else if (r == h- 1) cout << (char)(c + asciiAlpha); else if ((c < r || c > h - r) &&(c < h + r || c >= row - r)) cout << (char)(c + asciiAlpha); else cout << " "; } cout << "\n"; } return 0; }
Output : Enter rows: 20 A J T A J T AB IJK ST ABC HIJKL RST ABCD GHIJKLM QRST ABCDEFGHIJKLMN PQRST ABCDEFGHIJKLMNOPQRST ABCDEFGHIJKLMNOPQRST ABCDEFGHIJKLMNOPQRST
Are you seeking professional help for coding in the Java programming language? The tutorial of Java Programming Examples for beginners and experts will strongly improve your coding skills then you can program for any logic in Java.
Related Java Character Pattern Programs: