Program to Print Plus Star Pattern
In this article we are going to see how to print the Plus star pattern.
Example-1 When row value =4 * * * ******* * * *
Example-2 When row value=5 * * * * ********* * * * *
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 say
row
. - Take an inner loop to print the column values.
JAVA Code:
Method-1 : Static Star Character
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.print("Enter row value = "); int row=sc.nextInt(); // iterate through loop and print column values for(int r=1;r<=2*row-1;r++) { if(r!=row) // here the column values will be printed once for(int c=1;c<=row;c++) { if(c==row) System.out.print("*"); System.out.print(" "); } else // here the column values will be printed 2*row-1 times for(int c=1;c<=2*row-1;c++) { System.out.print("*"); } System.out.println(); } } }
Output: Enter row value = 5 * * * * ********* * * * *
Method-2 : Static Star Character
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.print("Enter row value = "); int row=sc.nextInt(); // Enter any Character System.out.print("Enter any character = "); char s=sc.next().charAt(0); // iterate through loop and print column values for(int r=1;r<=2*row-1;r++) { if(r!=row) // here the column values will be printed once for(int c=1;c<=row;c++) { if(c==row) System.out.print(s); System.out.print(" "); } else // here the column values will be printed 2*row-1 times for(int c=1;c<=2*row-1;c++) { System.out.print(s); } System.out.println(); } } }
Output: Enter row value = 5 Enter any character = + + + + + +++++++++ + + + +
Explanation
Let’s understand the program with detailed explanation.
Let we have taken row as 5.
Iteration-I
r=1 (passed through first for loop condition) which will execute till r<=row*2-1. It will check if r!=row, then it will enter nested for loop which will iterate from c=1 till row, where ever there is c==row a star symbol would be printed else a space would be printed i.e. 0 star, 4 spaces. Otherwise, another for loop will iterate from c=1 till row*2-1 and print star symbol i.e. 1 star (total 1 star). Inner loop iteration ends.
*
Iteration-II
r=2 (passed through first for loop condition) which will execute till r<=row*2-1. It will check if r!=row, then it will enter nested for loop which will iterate from c=1 till row, where ever there is c==row a star symbol would be printed else a space would be printed i.e. 0 star, 4 spaces. Otherwise, another for loop will iterate from c=1 till row*2-1 and print star symbol i.e. 1 star (total 1 star). Inner loop iteration ends.
*
Iteration-III
r=3 (passed through first for loop condition) which will execute till r<=row*2-1. It will check if r!=row, then it will enter nested for loop which will iterate from c=1 till row, where ever there is c==row a star symbol would be printed else a space would be printed i.e. 0 star, 4 spaces. Otherwise, another for loop will iterate from c=1 till row*2-1 and print star symbol i.e. 1 star (total 1 star). Inner loop iteration ends.
*
Iteration-IV
r=4 (passed through first for loop condition) which will execute till r<=row*2-1. It will check if r!=row, then it will enter nested for loop which will iterate from c=1 till row, where ever there is c==row a star symbol would be printed else a space would be printed i.e. 0 star, 4 spaces. Otherwise, another for loop will iterate from c=1 till row*2-1 and print star symbol i.e. 1 star (total 1 star). Inner loop iteration ends.
*
Iteration-V
r=5 (passed through first for loop condition) which will execute till r<=row*2-1. It will check if r!=row, then it will enter nested for loop which will iterate from c=1 till row, where ever there is c==row a star symbol would be printed else a space would be printed i.e. 0 star, 0 spaces. Otherwise, another for loop will iterate from c=1 till row*2-1 and print star symbol i.e. 9 star (total 9 star). Inner loop iteration ends.
*********
Iteration-VI
r=6 (passed through first for loop condition) which will execute till r<=row*2-1. It will check if r!=row, then it will enter nested for loop which will iterate from c=1 till row, where ever there is c==row a star symbol would be printed else a space would be printed i.e. 0 star, 4 spaces. Otherwise, another for loop will iterate from c=1 till row*2-1 and print star symbol i.e. 1 star (total 1 star). Inner loop iteration ends.
*
Iteration-VII
r=7 (passed through first for loop condition) which will execute till r<=row*2-1. It will check if r!=row, then it will enter nested for loop which will iterate from c=1 till row, where ever there is c==row a star symbol would be printed else a space would be printed i.e. 0 star, 4 spaces. Otherwise, another for loop will iterate from c=1 till row*2-1 and print star symbol i.e. 1 star (total 1 star). Inner loop iteration ends.
*
Iteration-VIII
r=8 (passed through first for loop condition) which will execute till r<=row*2-1. It will check if r!=row, then it will enter nested for loop which will iterate from c=1 till row, where ever there is c==row a star symbol would be printed else a space would be printed i.e. 0 star, 4 spaces. Otherwise, another for loop will iterate from c=1 till row*2-1 and print star symbol i.e. 1 star (total 1 star). Inner loop iteration ends.
*
Iteration-IX
r=9 (passed through first for loop condition) which will execute till r<=row*2-1. It will check if r!=row, then it will enter nested for loop which will iterate from c=1 till row, where ever there is c==row a star symbol would be printed else a space would be printed i.e. 0 star, 4 spaces. Otherwise, another for loop will iterate from c=1 till row*2-1 and print star symbol i.e. 1 star (total 1 star). Inner loop iteration ends.
*
Now r=10, so first for loop condition fails i.e. r<= row*2-1. And no more for loop will be executed. At last we will see a pattern like this on output screen.
* * * * ********* * * * *
C Code:
#include <stdio.h> int main() { int row; printf("Enter row value = "); scanf("%d",&row); for(int r=1;r<=row*2-1;r++) { if(r!=row) for(int c=1;c<=row;c++) { if(c==row) printf("*"); printf(" "); } else for(int c=1;c<=row*2-1;c++) { printf("*"); } printf("\n"); } }
Output: Enter row value = 5 * * * * ********* * * * *
C++ Code:
#include<iostream> using namespace std; int main() { int row; cout<<"Enter row value = "; cin>>row; for(int r=1;r<=row*2-1;r++) { if(r!=row) for(int c=1;c<=row;c++) { if(c==row) cout<<"*"; cout<<" "; } else for(int c=1;c<=row*2-1;c++) { cout<<"*"; } cout<<"\n"; } }
Output: Enter row value = 5 * * * * ********* * * * *
Related Java Star Pattern Programs: