Program to Print Alphabet S Star Pattern
In this article we are going to see how to print the alphabet S star pattern
Example-1 For rows = 10 ********** * * * * ********** * * * **********
Example-2 For rows = 5 ***** * ***** * *****
Now, let’s see the actual program to print it.
Want to excel in java coding? Practice with these Java Programs examples with output and write any kind of easy or difficult programs in the java language
Approach:
- Enter total row and store it in an integer variable
rows
. - Take one outer for loop to iterate the rows and one inner loop to iterate the columns.
- Print stars where conditions matches else continue.
JAVA Code:
Method-1: Static Star Character
import java.util.Scanner; class pattern { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.print("Rows : "); //Taking size as input from user int rows = scan.nextInt(); int r, c; for (r = 0; r < rows; r++) { //Outer Loop for (c = 0; c < rows; c++) { if((r==0||r==rows/2||r==rows-1)) System.out.print("*"); else if(r<rows/2 && c== 0) System.out.print("*"); else if(r>rows/2 && c==rows-1) System.out.print("*"); else System.out.print(" "); } System.out.println(); //Prints a newline } } }
Output: Rows : 5 ***** * ***** * *****
Method-2: User Input Character
import java.util.Scanner; class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); //Taking size as input from user System.out.print("Rows : "); int rows = scan.nextInt(); //Taking random character as input from user System.out.print("Character : "); char s = scan.next().charAt(0); for (int r = 0; r < rows; r++) { //Outer Loop for (int c = 0; c < rows; c++) { if((r==0||r==rows/2||r==rows-1)) System.out.print(s); else if(r<rows/2 && c== 0) System.out.print(s); else if(r>rows/2 && c==rows-1) System.out.print(s); else System.out.print(" "); } System.out.println(); //Prints a newline } } }
Output Rows : 5 Character : s sssss s sssss s sssss
Explanation :
Let’s understand the program by going through the detailed explanation.
We have taken rows value as 5.
Iteration-1
r=0, goes into the inner loop prints five stars for r==0
.
*****
Iteration-2
r=1, goes into the inner loop prints one star as r < rows / 2 && c == 0
.
*
Iteration-3
r=2, goes into the inner loop prints five stars as r == rows / 2
.
******
Iteration-4
r=3, goes into the inner loop prints one star as r > rows / 2 && c == rows - 1
.
*
Iteration-5
r=4, goes into the inner loop prints five star as r == rows - 1
.
******
After this r equals rows- i.e. 5 so program will come out of the loop.
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[]) { int rows; printf("Rows : "); scanf("%d", &rows); //Taking rows as input from user int r, c; for (r = 0; r < rows; r++) { //Outer Loop for (c = 0; c < rows; c++) { if ((r == 0 || r == rows / 2 || r == rows - 1)) printf("*"); else if (r < rows / 2 && c == 0) printf("*"); else if (r > rows / 2 && c == rows - 1) printf("*"); else printf(" "); } printf("\n"); //Prints a newline } return 0; }
Output: Rows : 5 ***** * ***** * *****
C++ Code:
#include <iostream> using namespace std; int main(int argc, char const *argv[]) { int rows; cout << "Rows : "; cin >> rows; //Taking rows as input from user int r, c; for (r = 0; r < rows; r++) { //Outer Loop for (c = 0; c < rows; c++) { if ((r == 0 || r == rows / 2 || r == rows - 1)) cout << "*"; else if (r < rows / 2 && c == 0) cout << "*"; else if (r > rows / 2 && c == rows - 1) cout << "*"; else cout << " "; } cout << endl; //Prints a newline } return 0; }
Output: Rows : 5 ***** * ***** * *****
Related Java Star Pattern Programs: