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:
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:
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:
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:
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:
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:
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:
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: