🎓 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
A square star pattern consists of stars (*) arranged in a grid format to form a square. Each row contains the same number of stars as the number of rows, making it a simple and easy-to-understand pattern. This exercise is useful for practicing loops and formatting output in JavaScript.
Problem Statement
Create a JavaScript program that:
- Accepts the size of the square (i.e., the number of rows and columns).
- Prints a square star pattern using stars (
*).
Example:
- Input:
size = 5 - Output:
***** ***** ***** ***** *****
Solution Steps
- Input the Size of the Square: The user specifies the size of the square (i.e., how many rows and columns it should have).
- Use Nested Loops: The outer loop handles the rows, and the inner loop handles printing the stars for each row.
- Display the Square Star Pattern: Print a fixed number of stars in each row based on the size.
JavaScript Program
// Step 1: Input the size of the square
let size = parseInt(prompt("Enter the size of the square: "));
// Step 2: Outer loop for rows
for (let i = 1; i <= size; i++) {
let output = '';
// Step 3: Inner loop to print stars for each row
for (let j = 1; j <= size; j++) {
output += '*';
}
// Print the output for the current row
console.log(output);
}
Explanation
Step 1: Input the Size of the Square
- The program starts by asking the user for the size of the square. The input is converted to an integer using
parseInt().
Step 2: Outer Loop for Rows
- The outer loop controls how many rows are printed. It runs from
1tosize, where each iteration represents a row.
Step 3: Inner Loop for Stars
- The inner loop controls how many stars (
*) are printed in each row. The number of stars printed in each row is equal tosize.
Step 4: Output the Row
- After constructing the row with stars, it is printed using
console.log().
Output Example
For size = 5, the output will be:
*****
*****
*****
*****
*****
For size = 4, the output will be:
****
****
****
****
Conclusion
This JavaScript program prints a square star pattern using nested loops. Each row contains a fixed number of stars, and the same pattern is repeated for the given number of rows. This exercise is useful for practicing loop control and output formatting in JavaScript.
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
Comments
Post a Comment
Leave Comment