Program to Print Windows Star Pattern
In this article we are going to see how to print window star program.
Example-1 When row value=5 * * * * * * * * * * * * * * * * * * * * *
Example-2: When row value=6 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Now, let’s see the actual program to print it.
Beginners and experienced programmers can rely on these Best Java Programs Examples and code various basic and complex logics in the Java programming language with ease.
Approach:
- Enter total row and store it in an integer variable
row
. - Calculate middle element.
- if
n
is odd we get 1 element . - in case of
n
is even we get 2 values.
- if
- 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 (r == 1 || c == 1 || r == row || c == row)
andif (r == a || c == a)
andif (r == b || c == b)
else it will print space .
- Take first inner for loop to print column value i.e., star according to condition
- 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,a,b; //creating object Scanner s = new Scanner(System.in); // entering the number of row System.out.print("Enter rows : "); row = s.nextInt(); // If n is odd then we will have only one middle element if (row % 2 != 0) { a = (row / 2) + 1; b = 0; } // If n is even then we will have two values else { a = (row / 2) + 1; b = row / 2 ; } for( r = 1; r <= row; r++) { for( c = 1; c <= row ; c++) { // If i,j equals to corner row or column then "*" if (r == 1 || c == 1 || r == row || c == row) System.out.print("* "); else { // If i,j equals to the middle row or column then "*" if (r == a || c == a) System.out.print("* "); else if (r == b || c == b) System.out.print("* "); else System.out.print(" "); } } System.out.println(); } } }
Output : Enter row : 5 * * * * * * * * * * * * * * * * * * * * *
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,a,b; char window; //creating 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 rows : "); window = s.next().charAt(0); // If n is odd then we will have only one middle element if (row % 2 != 0) { a = (row / 2) + 1; b = 0; } // If n is even then we will have two values else { a = (row / 2) + 1; b = row / 2 ; } for( r = 1; r <= row; r++) { for( c = 1; c <= row ; c++) { // If i,j equals to corner row or column then "*" if (r == 1 || c == 1 || r == row || c == row) System.out.print(window+" "); else { // If i,j equals to the middle row or column then "*" if (r == a || c == a) System.out.print(window+" "); else if (r == b || c == b) System.out.print(window+" "); else System.out.print(" "); } } System.out.println(); } } }
Output : Enter row : 5 Enter character : # # # # # # # # # # # # # # # # # # # # # #
C Code:
#include <stdio.h> int main() { int r, row, a,b,c ; printf("Enter rows: "); scanf("%d", &row); if (row % 2 != 0) { a = (row / 2) + 1; b = 0; } else { a = (row / 2) + 1; b = row / 2 ; } for( r = 1; r <= row; r++) { for( c = 1; c <= row ; c++) { if (r == 1 || c == 1 || r == row || c == row) printf("* "); else { if (r == a || c == a) printf("* "); else if (r == b || c == b) printf("* "); else printf(" "); } } printf ("\n"); } return 0; }
Output : Enter row : 5 * * * * * * * * * * * * * * * * * * * * *
C++ Code:
#include <iostream> using namespace std; int main() { int row, r , c ,a,b ; cout << "Enter rows: "; cin >> row; if (row % 2 != 0) { a = (row / 2) + 1; b = 0; } else { a = (row / 2) + 1; b = row / 2 ; } for( r = 1; r <= row; r++) { for( c = 1; c <= row ; c++) { if (r == 1 || c == 1 || r == row || c == row) cout << "* "; else { if (r == a || c == a) cout << "* "; else if (r == b || c == b) cout << "* "; else cout << " "; } } cout << "\n"; } return 0; }
Output : Enter row : 5 * * * * * * * * * * * * * * * * * * * * *
Related Java Star Pattern Programs: