×

Star Program in C Language

Star Program in C

Star patterns are a sequence of * or any other character used to construct any pattern or any like-square geometric form, triangle, hollow square, pyramid, rhombus, etc.  Many programmers worldwide highly recommend pattern issues to improve the skill of critical thought. For a beginner programmer, you should practice these patterns to get good hands-on the logical thinking and software flow control. In this exercise, a list of beginners and advanced Star patterns programmers are made to learn. 

Program to print square star pattern

#include <stdio.h>
 int main()
 {
     int k, m, s;
     /* Input number of rows from user */
     printf("Enter the number of rows: ");
     scanf("%d", &s); 
     /* Iterate through N rows */
     for(k=1; k<=s; k++)
     {
         /* Iterate over columns */
         for(m=1; m<=s; m++)
         {
             /* Print star for each column */ 
             printf("*");
         }
         /* Move to the next line/row */
         printf("\n");
     }
 } 

Output:

Star Program in C

Write a C-program to use loops to print a hollow square star pattern with diagonal. How to print a hollow square star pattern in the C system with diagonals. The logic for printing in C programming hollow square star pattern with diagonal.

Program to print Hollow Square with diagonal.

#include <stdio.h>
 int main()
 {
     int s, t, K;
     /* Input number of rows from user */
     printf("Enter the number of rows: ");
     scanf("%d", &K); 
     /* Iterate over each row */
     for(s=1; s<=K; s++)
     {
         /* Iterate over each column */
         for(t=1; t<=K; t++)
         {
             /*
              * Print star for,  
              * first row (s==1) or 
              * last row (s==K or
              * first column (t==1) or
              * last column (t==K) or 
              * row equal to column (s==t) or 
              * column equal to N-row (t==K-s+1)
              */
             if(s==1 || s==K || t==1 || t==K || s==t || t==(K - s + 1)) 
             {
                 printf("*");
             }
             else
             {
                 printf(" ");
             }
         } 
         /* Move to the next line */
         printf("\n");
     }
     return 0;
 } 

Output:

Star Program in C

Program to print rhombus star pattern

#include <stdio.h>
 int main()
 {
     int t,k, rows;
     /* Input number of rows from user */
     printf("Enter the No. of rows: ");
     scanf("%d", &rows); 
     for(t=1; t<=rows; t++)
     {
         /* Print trailing spaces */
         for(k=1; k<=rows - t; k++)
         {
             printf(" ");
         }
         /* Print stars after spaces */ 
         for(k=1; k<=rows; k++)
         {
             printf("*");
         }
         /* Move to the next line */
         printf("\n");
     }
     return 0; 
 } 

Output:

Star Program in C

Program to print mirrored right triangle star pattern.

#include <stdio.h>
 int main()
 {
     int k,l, rows;
     /* Input rows from user */
     printf("Enter any number of rows: ");
     scanf("%d", &rows); 
     /* Iterate through rows */
     for(k=1; k<=rows; k++)
     {
         /* Print spaces in decreasing order of row */
         for(l=k; l<rows; l++)
         {
             printf(" "); 
         }
         /* Print star in increasing order or row */
         for(l=1; l<=k; l++)
         {
             printf("*");
         }
         /* Move to next line */
         printf("\n"); 
     }
     return 0;
 } 

Output:

Star Program in C

Program to print pyramid star pattern series

#include <stdio.h>
 int main()
 {
     int w, s, rows;
     /* Input number of rows to print */
     printf("Enter number of rows : "); 
     scanf("%d", &rows);
     /* Iterate through rows */
     for(w=1; w<=rows; w++)
     { 
         /* Print leading spaces */
         for(s=w; s<rows; s++)
         {
             printf(" "); 
         }
         /* Print star */
         for(s=1; s<=(2*w-1); s++)
         {
             printf("*");
         }
         /* Move to next line */ 
         printf("\n");
     }
     return 0;
 } 

Output:

Star Program in C

Program to print reverse pyramid star pattern

#include <stdio.h>
 int main()
 {
     int p,n, rows;
     /* Input rows to print from user */
     printf("Enter number of rows : ");
     scanf("%d", &rows);
     for(p=1; p<=rows; p++) 
     {
         /* Print leading spaces */
         for(n=1; n<p; n++)
         {
             printf(" ");
         }
         /* Print stars */
         for(n=1; n<=(rows*2 -(2*p-1)); n++) 
         {
             printf("*");
         }
         /* Move to next line */
         printf("\n");
     }
     return 0;
 } 

Output:

Star Program in C

Program to print a hollow diamond star pattern.

#include <stdio.h>
 int main()
 {
     int h, t, r;
     printf("Enter the value of r : ");
     scanf("%d", &r);
     // Loop to print upper half of the pattern
     for(h=1; h<=r; h++) 
     {
         for(t=h; t<=r; t++)
         {
             printf("*");
         }
         for(t=1; t<=(2*h-2); t++) 
         {
             printf(" ");
         }
         for(t=h; t<=r; t++)
         {
             printf("*");
         }
         printf("\n"); 
     }
     // Loop to print lower half of the pattern
     for(h=1; h<=r; h++)
     {
         for(t=1; t<=h; t++)
         {
             printf("*"); 
         }
         for(t=(2*h-2); t<(2*r-2); t++)
         {
             printf(" ");
         }
         for(t=1; t<=h; t++)
         {
             printf("*"); 
         }
         printf("\n");
     }
     return 0;
 } 

Output:

Star Program in C

Program to print the right arrow star pattern.

#include <stdio.h>
 int main()
 {
     int s, t, n;
     // Input number of rows from user
     printf("Enter value of n : ");
     scanf("%d", &n);
     // Print the upper part of the arrow 
     for(s=1; s<n; s++)
     {
         // Print trailing (2*rownumber-2) spaces
         for(t=1; t<=(2*s-2); t++)
         {
             printf(" ");
         }
         // Print inverted right triangle star pattern 
         for(t=s; t<=n; t++)
         {
             printf("*");
         }
         printf("\n");
     }
     // Print lower part of the arrow 
     for(s=1; s<=n; s++)
     {
         // Print trailing (2*n - 2*rownumber) spaces
         for(t=1; t<=(2*n - 2*s); t++)
         {
             printf(" ");
         }
         // Print simple right triangle star pattern 
         for(t=1; t<=s; t++)
         {
             printf("*");
         }
         printf("\n");
     }
     return 0;
 } 

Output:

Star Program in C

Related Topics

Const vs Volatile in C

Introduction Qualifiers are nothing but keywords which are used to modify the properties of a variable. Const and Volatile keywords are qualifiers in C. The Const qualifier is applied to the...

4 minutes read.

getch() function in C

getch() is a pre-define or built-in function present in the conio.h library. It returns the given character immediately without waiting for the enter key to be entered. By using getch()...

3 minutes read.

One Dimensional Array in C

One Dimensional Array in C: In the C programming language, an array is defined as a collection of one or more values of the same type. Every value present in...

4 minutes read.

Length of an Array Function in C

In C, there is no built-in function to get the length of an array. However, there are a few ways you can determine the length of an array. It is necessary...

15 minutes read.

Assignment Operator Program in C

An assignment operator is a symbol used to assign a value to a variable in a programming language. The assignment operator is often the equal symbol (=) in programming languages....

12 minutes read.

Displaying Array in C

Reference a collection of variables of similar data type in an array using a single element. The parts are stored next to each other. When declaring an array, you must specify...

4 minutes read.

Ftell() Function in C

Ftell(): In File Handling we have some special functions like Ftell(), Fseek(), rewind() etc.. while you are randomly accessing the file these functions play very important role and these functions...

3 minutes read.

While Loop Syntax in C

What is Loop? The statements in the sequence are repeatedly executed via looping statements in C until the condition is met. The body of a loop and a control statement make...

4 minutes read.

Palindrome Number in C

What is a Palindrome? A number that doesn't change when reversed is known as a palindrome. We reverse a number and compare it to the original number to determine whether it is...

4 minutes read.

Else If Ladder in C

What is Else If Ladder: When there are multiple options and a user has to decide from the options, we use Else If Ladder. Basically, it is an extension of if...

3 minutes read.

pow() function in C

pow() function is one of the in-built functions present in math.h header file Power function is used to calculate the powers of the given number. The syntax of the power...

3 minutes read.

Escape Sequence in C

An escape sequence is a sequence of character that used inside the string literal or character.  All the escape sequence is used with the backslash (\) symbol. The list of an...

1 minute read.

Format Specifier in C

Format Specifier in C Format specifiers can be described as an operator that is used to print the data referred by any object or variable in combination with the printf ()...

3 minutes read.

Data Structures And Algorithms in C

C language is one of the most flexible and simple languages. So, it is always better to learn any concept in C language compared to other languages. Let us look...

13 minutes read.

C Array

An array is a collection of elements that are used to hold the fixed number of values of the same type. We cannot change the size and type of array...

2 minutes read.

Difference between If Else and Nested If Else in C

What is the If Else statement in C? In C programs, if else conditions are used to perform some operations based on specific conditions. The operation is only executed if the...

6 minutes read.

C program to Store Information of Students Using Structure

What is the Structure in C? User-defined data types include structures. Structures aid your ability to combine things of various categories into a single group. Like arrays, it operates similarly. A...

3 minutes read.

Compilation Process in C language

What is the Compilation Process?  The compilation is a method whereby the source code is converted into object code. It is achieved with compiler assistance. The compiler tests the source code for syntactic...

3 minutes read.

Errors in C

Errors in C Errors are nothing but problems or faults that pretty much occur in all the programming languages. Errors make the behavior of the program seem abnormal, and even the...

4 minutes read.

Nested loop in C

Definition of Nested Loop A nested loop is generally used when we want to run a loop statement inside another loop statement. This kind of loop is also known as a...

3 minutes read.