Program to Print Alphabet Y Star Pattern
In this article we are going to see how to print Y star program or how to print y pattern in java.
Example-1 When row value=4 * * * * * *
Example-2: When row value=5 * * * * ** * *
Now, let’s see the actual program printing it, java program to print pattern.
Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java and get a good grip on it for better results in exams or interviews.
Approach:
- Enter total row and store it in an integer variable
row
. - Take first for loop to print the row value and a star for each row .
- Take a counter value and assign it to 0.
- Take first inner for loop to print column value i.e., star or spaces according to condition
if (c==d || c==row-d && r<=row/2)
. - For each condition
if(r < row/2)
increase the counter value . - 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,d=0; //creating object Scanner s = new Scanner(System.in); // entering the number of row System.out.print("Enter rows : "); row = s.nextInt(); //outer for loop for (r = 0; r < row ; r++) { for (c = 0; c <= row ; c++) { if (c == d || c == row - d && r <= row / 2) System.out.print("*"); else System.out.print(" "); } System.out.print("\n"); if (r < row / 2) d++; } } }
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,d=0; //creating object Scanner s = new Scanner(System.in); // entering the number of row System.out.print("Enter rows : "); row = s.nextInt(); // entering any character System.out.print("Enter character : "); char y = s.next().charAt(0); //outer for loop for (r = 0; r < row ; r++) { for (c = 0; c <= row ; c++) { if (c == d || c == row - d && r <= row / 2) System.out.print(y); else System.out.print(" "); } System.out.print("\n"); if (r < row / 2) d++; } } }
Output : Enter row : 5 Enter character : @ @ @ @ @ @@ @ @
C Code:
#include <stdio.h> int main() { int r, row, c ,d; printf("Enter rows: "); scanf("%d", &row); for (r = 0; r < row ; r++) { for (c = 0; c <= row ; c++) { if (c == d || c == row - d && r <= row / 2) printf("*"); else printf(" "); } printf("\n"); if (r < row / 2) d++; } return 0; }
Output : Enter row : 5 * * * * ** * *
C++ code:
#include <iostream> using namespace std; int main() { int row, r , c ,d ; cout << "Enter rows: "; cin >> row; for (r = 0; r < row ; r++) { for (c = 0; c <= row ; c++) { if (c == d || c == row - d && r <= row / 2) cout << "*"; else cout << " "; } cout << "\n"; if (r < row / 2) d++; } return 0; }
Output : Enter row : 5 * * * * ** * *
Recommended Reading On: Java Program to Print Alphabet Z Star Pattern
Check once if you understand the topic or not:
- Print the alphabet a to z in star pattern
- Print the alphabet a to z in star pattern
- Program to print alphabet a using stars in c
- Program to print alphabets using stars in python
- Java program to print alphabet pattern of stars
- Print alphabet pattern in c
- How to print name in star pattern in java
- Write a program to draw the character pattern m in pythoN.
Related Java Star Pattern Programs:
Java Program to Print Alphabet A Star Pattern
- Java Program to Print Alphabet X Star Pattern
- Java Program to Print Alphabet T Star Pattern
- Java Program to Print Alphabet Q Star Pattern
- Java Program to Print Alphabet I Star Pattern
- Star pattern program in java.
- Pattern printing in java.
- Pattern programs in java
- Star patterns in java.