📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
Printing an X
pattern is a fun programming exercise that involves controlling the placement of characters based on their row and column indices. This guide will walk you through writing a Java program that prints an X
pattern using stars (*
), where the diagonal elements form the shape of the X
.
Problem Statement
Create a Java program that:
- Accepts the size (number of rows and columns) of the
X
pattern. - Prints an
X
pattern where stars (*
) are placed on both diagonals and spaces are placed elsewhere.
Example:
Input:
5
Output:
* * * * * * * * *
Input:
7
Output:
* * * * * * * * * * * * *
Solution Steps
- Take Input: Accept the size of the
X
pattern (must be an odd number for symmetry). - Print the
X
Pattern: Use nested loops to print stars (*
) for diagonal positions and spaces elsewhere. - Display the Result: Print the
X
pattern on the console.
Java Program
// Java Program to Print a Cross (X) Pattern // Author: https://www.rameshfadatare.com/ import java.util.Scanner; public class CrossPattern { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Step 1: Get the size of the pattern System.out.print("Enter the size of the X pattern (odd number): "); int size = scanner.nextInt(); // Step 2: Loop through each row for (int i = 0; i < size; i++) { // Step 3: Loop through each column in the row for (int j = 0; j < size; j++) { // Step 4: Print '*' on the diagonals, otherwise print ' ' if (j == i || j == (size - i - 1)) { System.out.print("*"); } else { System.out.print(" "); } } // Move to the next line after printing each row System.out.println(); } // Close the scanner scanner.close(); } }
Explanation
Step 1: Take Input
- The program asks the user for the size of the
X
pattern. The size must be an odd number to create a symmetricX
pattern.
Step 2: Loop Through Each Row
- A
for
loop iterates from0
tosize - 1
, where each iteration corresponds to one row of the pattern.
Step 3: Loop Through Each Column
- For each row, a nested
for
loop iterates from0
tosize - 1
to print the elements in that row.
Step 4: Print Stars (*
) on the Diagonals
- The condition
j == i
prints a star on the primary diagonal (top-left to bottom-right). - The condition
j == (size - i - 1)
prints a star on the secondary diagonal (top-right to bottom-left). - Elsewhere, spaces (
" "
) are printed.
Output Example
For an input of 5
, the program outputs:
* * * * * * * * *
For an input of 7
, the program outputs:
* * * * * * * * * * * * *
Example with Different Input
For an input of 3
, the program outputs:
* * * * *
Conclusion
This Java program demonstrates how to print an X
pattern using nested loops. By carefully controlling the conditions for printing stars (*
) on both diagonals, the program efficiently creates the desired cross shape. This exercise is a great way to practice using loops and conditionals in Java programming for pattern generation.
Comments
Post a Comment
Leave Comment