Program to Print Crown Star Pattern
In this article we are going to see how to print crown star program.
Example-1 When row value=15 * * * * * * ** *** ** *** ***** *** *************** *************** ***************
Example-2 When row value=20 * * * * * * ** *** ** *** ***** *** **** ******* **** ******************** ******************** ******************** ********************
Now, let’s see the actual program to print it.
Are you a job seeker and trying to find simple java programs for Interview? This would be the right choice for you, just tap on the link and start preparing the java programs covered to crack the interview.
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 a star for each row .
- Take first inner for loop to print column value i.e., star according to condition
if (c == 0 || c == h || c == row – 1) and (r == h- 1)and
if ((c < r || c > h – r) &&(c < h + r || c >= row – r)) else print the spaces . - Then go on printing the star symbol according to loop.
JAVA Code:
Method-1 : Static Star Character
import java.util.*;
public class Main
{
public static void main(String args[])
{
// taking variable for loop iteration and row .
int row,r,c,h;
//creating scanner class object
Scanner s = new Scanner(System.in);
// entering the number of row
System.out.print("Enter rows : ");
row = s.nextInt();
h= (row-1)/2;
for (r = 0; r < h ; r++)
{
// printing stars of the crown
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)
System.out.print("*");
else
System.out.print(" ");
}
else if (r == h- 1)
System.out.print("*");
else if ((c < r || c > h - r) &&(c < h + r || c >= row - r))
System.out.print("*");
else
System.out.print(" ");
}
// taking to the new line
System.out.println();
}
}
}
Output : Enter row : 20 * * * * * * ** *** ** *** ***** *** **** ******* **** ******************** ******************** ******************** ********************
Method-2 : User Input Character
import java.util.*;
public class Main
{
public static void main(String args[])
{
// taking variable for loop iteration and row .
int row,r,c,h;
char crown;
//creating scanner class object
Scanner s = new Scanner(System.in);
// entering the number of row
System.out.print("Enter rows : ");
row = s.nextInt();
// entering any random character
System.out.print("Enter character : ");
crown = s.next().charAt(0);
h= (row-1)/2;
for (r = 0; r < h ; r++)
{
// printing stars of the crown
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)
System.out.print(crown);
else
System.out.print(" ");
}
else if (r == h- 1)
System.out.print(crown);
else if ((c < r || c > h - r) &&(c < h + r || c >= row - r))
System.out.print(crown);
else
System.out.print(" ");
}
// taking to the new line
System.out.println();
}
}
}
Output : Enter row : 20 Enter Character : * * * * * * * ** *** ** *** ***** *** **** ******* **** ******************** ******************** ******************** ********************
C Code:
#include <stdio.h>
int main() {
int r, row, h,c ;
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)
{
// print '*' at first middle and last column
if (c == 0 || c == h || c == row - 1)
printf("*");
else
printf(" ");
}
else if (r == h- 1)
printf("*");
else if ((c < r || c > h - r) &&(c < h + r || c >= row - r))
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}
Output : Enter row : 20 * * * * * * ** *** ** *** ***** *** **** ******* **** ******************** ******************** ******************** ********************
C++ Code:
#include <iostream>
using namespace std;
int main()
{
int row, r , c ,h;
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 << "*";
else
cout << " ";
}
else if (r == h- 1)
cout << "*";
else if ((c < r || c > h - r) &&(c < h + r || c >= row - r))
cout << "*";
else
cout << " ";
}
cout << "\n";
}
return 0;
}
Output : Enter row : 20 * * * * * * ** *** ** *** ***** *** **** ******* **** ******************** ******************** ******************** ********************
Related Java Star Pattern Programs: