🎓 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 (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
Printing a plus (+) pattern using stars (*) involves printing a cross shape, where the middle row and the middle column are filled with stars. This is a good exercise for practicing pattern generation using nested loops in Java.
Problem Statement
Create a Java program that:
- Accepts the size of the plus pattern.
- Prints a plus-shaped pattern using stars (
*).
Example:
Input:
5Output:
* * ***** * *Input:
7Output:
* * * ******* * * *
Solution Steps
- Take Input: Define the size of the plus pattern.
- Print the Plus Pattern: Use nested loops to print stars (
*) in the middle row and middle column. - Display the Result: Print the plus pattern on the console.
Java Program
// Java Program to Print a Plus (+) Pattern
// Author: https://www.rameshfadatare.com/
import java.util.Scanner;
public class PlusPattern {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Step 1: Get the size of the plus pattern
System.out.print("Enter the size of the plus pattern (odd number): ");
int size = scanner.nextInt();
// Middle index calculation (since the pattern is symmetric)
int mid = size / 2;
// Step 2: Print the plus pattern using nested loops
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
// Step 3: Print stars for the middle row or middle column
if (i == mid || j == mid) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println(); // Move to the next line after printing each row
}
// Close the scanner
scanner.close();
}
}
Explanation
Step 1: Take Input
- The program prompts the user for the size of the plus pattern. The size must be an odd number for the pattern to be symmetric.
Step 2-3: Print the Plus Pattern
- A nested loop structure is used to iterate through each row (
i) and each column (j). - The condition
if (i == mid || j == mid)ensures that stars (*) are printed for the middle row (i == mid) and the middle column (j == mid). - All other positions are filled with spaces (
" ").
Output Example
For an input of 5, the program prints:
*
*
*****
*
*
For an input of 7, the program prints:
*
*
*
*******
*
*
*
Example with Different Input
For an input of 9, the program outputs:
*
*
*
*
*********
*
*
*
*
Demo
Conclusion
This Java program demonstrates how to print a plus (+) pattern using nested loops. By controlling the placement of stars and spaces based on row and column indices, the program is able to create the symmetric plus shape. This exercise is a good way to practice loop manipulation and pattern generation in Java.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
%20Pattern.png)
Comments
Post a Comment
Leave Comment