×

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

Comma Operator in C

What is a comma operator? The comma operator in the C programming language has the least priority. The comma operator is essentially a binary operator that operates on the first operand...

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.

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.

Prime Number code in C

Prime Number Programs in C language In this tutorial, we will learn how to check whether the given number is a prime number or not, and how to print all the...

4 minutes read.

Flow Chart of Do while loop in C

This is a flowchart that represents the process of executing the Do while loop in the C programming language Generally, as we know there are three main components of Do While...

3 minutes read.

Library Functions in C

What are library functions? Library functions are the built-in functions (functions provided by the system) which are stored in the library. The library is the common location where these functions are...

3 minutes read.

Why does sizeof(x++) not Increment x in C

Sizeof() is a much-involved operator in C or C++. It is an incorporated time unary administrator which can be utilized to figure the size of its operand. The consequence of...

5 minutes read.

Associativity of Operators in C

What is an Associativity operator? Two operators with equal priority are connected by employing their association when they are present in an expression. There are two different types of associativity: left to rightright...

4 minutes read.

C Programming Tutorial

What is C Programming C is most popular and widely used computer programming language. It was developed by Dennis Ritchie in 1972 at the Bell Lab. It is used to develop system application software. There...

8 minutes read.

Array to function in C

Array to function in C We can create functions that receive an array as an argument. To pass an array into a function, we need to write the name of the...

4 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.

Diffie-Hellman Algorithm in C

Background: A method of public-key encryption known as elliptic curve cryptography (ECC) is based on the algebraic structure of elliptic curves over finite fields. To ensure equal security, ECC encryption requires fewer...

4 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.

Call Back Function in Embedded C

Callback function are one the most remarkable systems in C. A callback function is any code that is passed as a contention to another code, so this is the last...

3 minutes read.

Addition of Matrix in C

The mathematical operation of includingor greater matrices is called the addition of matrices. A matrix is a square series of numbers, symbols, words, letters, and different things. In this article,...

3 minutes read.

Use of free() function in C

Introduction The free() function uses in C programming language. The free() function in the C programming language uses to release or deallocate the memory blocks; these blocks are previously allocated by calloc(), malloc() or realloc()...

3 minutes read.

memmove() in C

Introduction: In this article we are discuss about the memmove() function in C. This function transfers memory blocks from one location to another. The memmove() function is declared in the...

3 minutes read.

Prime Number Program in C using for Loop

In this article, we will know about the procedure of checking whether a natural number inputted by the user is a prime number or non-prime number. Definition of the prime number A...

3 minutes read.

For Loop in C Programming Examples

The Syntax of the For Loop for (initialization statement; termination condition; modifying (increment/ decrement) statement) {       /* main body of the For loop */     } In for loop, the initialization command...

6 minutes read.

First Fit Program in C

This is one of the easiest ways to allocate memory. The main goal is to divide the memory into several fixed sizes. In C, there is only one process for...

3 minutes read.