Star Pattern Programs in C

Star Pattern Programs in C

5 mins readComment
Esha
Esha Gupta
Associate Senior Executive
Updated on Sep 3, 2024 13:53 IST

Pattern programs in C are a type of problem that uses loops to produce different patterns of numbers, stars (*), or other characters. This blog will dive deeper into Star Pattern Program C programming language. Let's understand more!

Star pattern programs in C are popular for practising nested loop control structures. They involve using loops to print out a particular pattern of stars (*) on the screen.

Learning Loops in C
Learning Loops in C
In C programming, a loop is a control structure that allows a set of instructions to be executed repeatedly based on a specified condition. Loops provide an efficient way to...read more

Introduction to For Loop in C
Introduction to For Loop in C
A loop is one of the most fundamental logical structures in computer programming. Defining loops in code allows computers to repeat specific tasks. Defining the loop in a computer program...read more

All About While Loop in C
All About While Loop in C
Have you ever wondered how a while loop works in C programming? A control flow statement allows code to be executed repeatedly based on a given Boolean condition. The while...read more

What is Do While Loop in C?
What is Do While Loop in C?
In computer programming, loops play an essential part. Instead of writing hundreds of lines of code that keeps running until a particular condition is met, you can do the exact...read more
 

Basic Approach to Solve a Star Pattern Program in C Using an Example 

A step-by-step guide using a simple example: Cross Star Pattern

 

Steps to Solve

1. Understand the Pattern

The cross star pattern consists of a central row and column filled with stars, creating a cross shape in a square grid.

2. Initialize the Loop for Rows

Set up a loop for the rows of the pattern. For a cross of length 7, we have


 
int n = 7; // Total rows and columns (must be odd)
Copy code

3. Nested Loop for Printing Stars and Spaces.

Use nested loops to manage the stars and spaces.


 
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
// Print a star if in the center row or center column, otherwise print a space
if (i == n / 2 || j == n / 2) {
printf("*");
} else {
printf(" ");
}
}
printf("\n");
}
Copy code

Complete Code


 
#include <stdio.h>
int main() {
int n = 7; // Length of the cross (must be odd)
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
// Center row or center column
if (i == n / 2 || j == n / 2) {
printf("*");
} else {
printf(" ");
}
}
printf("\n");
}
return 0;
}
Copy code

This basic approach can be adapted to solve various other star pattern programs in c.

Let's check a video on the same

Source : Meganadha Reddy

Recommended online courses

Best-suited C / C++ courses for you

Learn C / C++ with these high-rated online courses

β‚Ή4.24 K
6 weeks
β‚Ή475
6 weeks
– / –
45 hours
β‚Ή4 K
80 hours
– / –
1 month
– / –
40 hours
– / –
2 months
– / –
1 year
β‚Ή4 K
80 hours

Top C Program to Print Star Pattern

  1. Square Star Pattern
  2. Inverted Pyramid Star Pattern
  3. Pyramid Star Pattern
  4. Diamond Star Pattern
  5. Hollow Square Star Pattern

Let’s understand each of these one by one in detail

1. Square Star Pattern: This pattern forms a square with stars.

Code


 
#include <stdio.h>
int main() {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
printf("*");
}
printf("\n");
}
return 0;
}
Copy code

2. Inverted Pyramid Star Pattern: This pattern forms an inverted pyramid with stars.

Code

   
#include <stdio.h>
int main() {
for (int i = 0; i < 5; i++) {
// Printing spaces
for (int j = 0; j < i; j++) {
printf(" ");
}
// Printing stars
for (int j = i; j < 5; j++) {
printf("* ");
}
printf("\n");
}
return 0;
}
Copy code

3. Pyramid Star Pattern: This pattern forms a pyramid with stars.

Code


 
#include <stdio.h>
int main() {
for (int i = 0; i < 5; i++) {
// Printing spaces
for (int j = 5; j > i; j--) {
printf(" ");
}
// Printing stars
for (int k = 0; k < i + 1; k++) {
printf("* ");
}
printf("\n");
}
return 0;
}
Copy code

4. Diamond Star Pattern: This pattern prints a diamond shape with stars.

Code


 
#include <stdio.h>
int main() {
int n = 5;
// Upper half of the diamond
for (int i = 0; i < n; i++) {
// Printing spaces
for (int j = n - 1; j > i; j--) {
printf(" ");
}
// Printing stars
for (int k = 0; k < i + 1; k++) {
printf("* ");
}
printf("\n");
}
// Lower half of the diamond
for (int i = 1; i < n; i++) {
// Printing spaces
for (int j = 0; j < i; j++) {
printf(" ");
}
// Printing stars
for (int k = n - 1; k >= i; k--) {
printf("* ");
}
printf("\n");
}
return 0;
}
Copy code

5. Hollow Square Star Pattern: This pattern prints a square but leaves the middle portion hollow.

 

Code


 
#include <stdio.h>
int main() {
int n = 5;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
// Print stars at the borders
if (i == 0 || i == n - 1 || j == 0 || j == n - 1) {
printf("*");
} else {
// Print spaces in the middle
printf(" ");
}
}
printf("\n");
}
return 0;
}
Copy code

Control Statements in C | Meaning and Types
Control Statements in C | Meaning and Types
Control statements in C are used to determine the order in which the instructions within a program are executed. They are of three types: Selection, Iteration & Jump statements. Think...read more

Number Pattern Programs in Java
Number Pattern Programs in Java
Pattern programs in Java are a type of problem that use nested loops to produce different patterns of numbers, stars (*), or other characters. In this blog we will dive...read more

Star Pattern Programs in Java
Star Pattern Programs in Java
Pattern programs in Java are a type of problem that uses nested loops to produce different patterns of numbers, stars (*), or other characters. In this blog, we will dive...read more

Number Pattern Programs in C
Number Pattern Programs in C
Pattern programs in C are a type of problem that use nested loops to produce different patterns of numbers, stars (*), or other characters. In this blog, we will dive...read more

Number Pattern in Python
Number Pattern in Python
Pattern programs in Python are a type of problem that uses nested loops to produce different patterns of numbers, stars (*), or other characters. In this blog, we will dive...read more

Top 10 Pattern Programs in C
Top 10 Pattern Programs in C
Have you ever wondered how intricate patterns can be created with simple loops in C? Pattern programs in C are a fascinating way to learn about nested loops and control...read more

Star Pattern in Python
Star Pattern in Python
Pattern programs in Python are a type of problem that uses nested loops to produce different patterns of numbers, stars (*), or other characters. In this blog, we will dive...read more

Conclusion

Thus, the star pattern in C is essential to foundational programming learning and practice. They help understand the use and manipulation of loops and control statements and enhance logical thinking and problem-solving skills.

FAQs

What is a Star Pattern Program in C?

A star pattern program in C is a type of programming exercise that involves printing a pattern of asterisks (*) on the screen. These patterns can form various shapes, such as triangles, squares, or diamonds. They are commonly used to practice nested loops and control statements in C programming.

Why are Star Pattern Programs Popular in Learning C?

Star pattern programs are popular in learning C because they help beginners understand the concepts of loops and nested loops. They also aid in grasping how to control the flow of a program using conditional statements. These programs are simple yet effective for building logic and problem-solving skills.

What are Some Common Types of Star Patterns in C?

Common types of star patterns include the right-angled triangle, inverted triangle, pyramid, inverted pyramid, diamond, and more. Each of these patterns requires a different approach with loops and conditions, providing a variety of challenges for learners.

Can Star Pattern Programs in C be Customized?

Yes, star pattern programs in C can be customized in various ways. You can change the size of the pattern, invert the shapes, or even combine two or more patterns to create more complex designs. Creativity can be applied in spacing, alignment, and combining characters other than the asterisk.

What Challenges Might Beginners Face When Writing Star Pattern Programs in C?

Beginners might face challenges in understanding the nesting and scope of loops, determining the number of spaces or stars per line, and ensuring the pattern aligns correctly. In more complex patterns, figuring out the logic and the relationship between rows and columns of stars can also be challenging. Practice and understanding the basic principles of loops and conditionals are key to overcoming these challenges.

About the Author
author-image
Esha Gupta
Associate Senior Executive

Hello, world! I'm Esha Gupta, your go-to Technical Content Developer focusing on Java, Data Structures and Algorithms, and Front End Development. Alongside these specialities, I have a zest for immersing myself in v... Read Full Bio