Program to Print Cross Symbol Star Pattern
In this article we are going to see how to print the cross symbol star pattern.
Example-1 When size value=5 * * * * * * * * *
Example-2: When size value=10 * * * * * * * * ** ** * * * * * * * *
Now, let’s see the actual program to print it.
If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available.
Approach:
- Enter the size of the square and store it in an integer variable
size
. - Take outer for loop to iterate the rows.
- Take second/inner for loop to iterate the columns.
- Then go on printing the star symbols according to the iteration.
JAVA Code:
Method-1 : Static Star Character
import java.util.Scanner; class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.print("Size : "); //Taking size as input from user int r, c, size=scan.nextInt(); //Outer loop for (r = 0; r < size; ++r) { //Outer for loop for (c = 0; c < size; ++c) { //Inner for loop if(r==c || c==(size-r-1)) //Condition to check to print star System.out.print("*"); else System.out.print(" "); } //Prints a new line System.out.println(); } } }
Output Size : 5 * * * * * * * * *
Method-2 : User Input Character
import java.util.Scanner; class Main { public static void main(String[] args) { int r, c,size; Scanner scan = new Scanner(System.in); System.out.print("Size : "); //Taking size as input from user size=scan.nextInt(); System.out.print("Character : "); //Taking character as input from user char ch=scan.next().charAt(0); //Outer loop for (r = 0; r < size; ++r) { //Outer for loop for (c = 0; c < size; ++c) { //Inner for loop if(r==c || c==(size-r-1)) //Condition to check to print star System.out.print(ch); else System.out.print(" "); } //Prints a new line System.out.println(); } } }
Output Size : 5 Character : x x x x x x x x x x
Explanation :
Let’s understand the program by going through the detailed explanation.
We have taken size value as 5.
Iteration-1
r=0 (passes the first for loop condition) because it will execute until r<size
Now inner for loop will print a star for r==c
followed by 3 spaces from else statement, then another star for
.c==(size-r-1)
* *
Iteration-2
r=1 (passes the first for loop condition) because it will execute until r< size
Now inner for loop will print one space from else statement then a star for r==c
followed by 1 space from else statement, then another star for c==(size-r-1)
followed by space.
* *
Iteration-3
r=2 (passes the first for loop condition) because it will execute until r< size
Now inner for loop will print 2 spaces then a star for r==c
and remaining spaces.
*
Iteration-4
r=3 (passes the first for loop condition) because it will execute until r< size
Now inner for loop will print one space from else statement then a star for
followed by 1 space from else statement, then another star for c==(size - r -1)
r==c
followed by space.
* *
Iteration-5
r=4 (passes the first for loop condition) because it will execute until r< size
Now inner for loop will print a star for c==(size-r-1)
followed by 3 spaces from else statement, then another star for r==c
.
* *
Now r=5, so first for loop condition will fail. So next for loop will not be executed any more.
Now, after end of all iteration we will see the complete pattern is printed on the output screen like this.
* * * * * * * * *
C Code:
#include <stdio.h> int main(int argc, char const *argv[]) { printf("Size : "); //Taking size of the square as input from user int size, r, c; scanf("%d", &size); //Outer loop for (r = 0; r < size; ++r) { //Outer for loop for (c = 0; c < size; ++c) { //Inner for loop if (r == c || c == (size - r - 1)) //Condition to check to print star printf("*"); else printf(" "); } //Prints a new line printf("\n"); } return 0; }
Output Size : 5 * * * * * * * * *
C++ Code:
#include <iostream> using namespace std; int main(int argc, char const *argv[]) { cout << "Size : "; //Taking height Of Tree as input from user int size, r, c; cin >> size; //Outer loop for (r = 0; r < size; ++r) { //Outer for loop for (c = 0; c < size; ++c) { //Inner for loop if (r == c || c == (size - r - 1)) //Condition to check to print star cout << "*"; else cout << " "; } cout << endl; } return 0; }
Output Size : 5 * * * * * * * * *
Related Java Star Pattern Programs: