In this tutorial you will learn about the C Program to Print Diamond Star Pattern and its application with practical example.
C Program to Print Diamond Pattern.
In this tutorial, we will learn to create a C program that will print a Diamond Star Pattern using C programming.
Prerequisites
Before starting with this tutorial, we assume that you are the 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 diamond:-
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. With the help of the c programming, we will create a diamond pattern. First, we will take the number of rows in the input, and then we will print the pattern.
In this program, we will learn to create diamond-like patterns with the help of c programming.
1 2 3 4 5 6 7 8 9 10 11 |
Step 1: Start the program. Step 2: Declaring the required variables. Step 3: Taking the input number of rows. Step 4: Generating the pattern using the loops. Step 5: Printing the pattern using the print function. Step 6: End the program. |
Program:-
To create a diamond 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 |
#include <stdio.h> int main() { // declaring the local variables int i, j, r, k; printf (" Enter a number of rows for the shape: \n "); scanf("%d", &r); int space = r-1, num=1; printf("\n"); //first for loop for lines creation parent for loop for (i = 1; i <= r; i++) { //child for loop for printing the blank spaces for (j = 1; j <= space; j++) { printf(" "); } //for loop to print the * diamond pattern for ( k= 1; k <= num; k++) { printf("*"); } if(space > i) { space = space -1; num = num+2; } if(space <i) { space = space + 1; num = num -2; } printf("\n"); } return(0); } |
Output:-
In the above program, we have first initialized the required variable.
- i = it will hold the integer value to control the parent for loop.
- j = it will hold the integer value to control the child for a loop.
- k = it will hold the integer value to control child for loop.
- r = it will hold the integer value for the number of rows of pattern.
Initializing the first parent for loop size and number of rows.
For Loop to print the blank spaces.
For Loop to print the “ * ”.
Main Program Code.