In this tutorial you will learn about the C Program to Print Star Pattern and its application with practical example.
C Program to Print Star Pattern
In this tutorial, we will learn to create a C program that will Create a Star pattern using C programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
- For loop in C Programming.
Create a Star pattern:-
The C language is a very powerful programming language. In C programming we can perform many operations with the help of codings. The c language is very easy to create any pattern. In this program, we will learn to create star-like patterns with the help of some code.
Algorithm:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
STEP 1: START STEP 2: first declare the variables STEP 3: then start the parent for loop STEP 4: now make the child for loop STEP 5: in this child loop print the blank spaces STEP 6: now use another child loop to start drawing the <strong>Star </strong><strong>pattern</strong> STEP 7: Now this for loop will also execute under the parent for loop STEP 8: Print "\n" for changing the line before the increment of the main loop STEP 9: increment of the main for loop STEP 10: return the zero value for main function |
Program:-
To Create a Star pattern
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
#include <stdio.h> #include <stdlib.h> #include <math.h> int main() { // declaring the variables for the program float size,i,j,q,w; q=tan(M_PI*0.4); w=tan(M_PI*0.2); //Taking the size of the star user wanted to create printf("Enter the size for the pattern :- \n"); scanf("%f",&size); for(j=ceil(size*q);j>=0;j--) { for(i=-ceil(0.55*size*q/w-size);i<ceil(0.55*size*q/w-size);i++) { if((j<=0.55*size*q && j>=(i+size)*w && j>=(size-i)*w)||(j>=(i+size)*w && j<=(i+size)*q && j<=(size-i)*q)||(j<=(size-i)*q && j>=(size-i)*w && j<=(i+size)*q)) { //printing the pattern * at the places printf("*"); } else{ //printing blank spaces printf(" "); } } //Printing Line Break printf("\n"); } return 0; } |
Output:-
The above program we have first initialized the required variable.
- i = it will hold the integer value to control parent for loop.
- j = it will hold the integer value to control child for loop.
- k = it will hold the integer value to control child for loop.
- size = it will hold the integer value for the number of rows of pattern.
Taking the size of the pattern.
Importing required header files.
Initializing the for loop size and number of rows.