** Write a C Program to Print Diamond Pattern**.

Example, Input5Output* *** ***** *** *Input6Output* *** ***** ***** *** *

## Explanation

- First, we print the Upper Triangle.
- The Upper Triangle consists of
**N/2 + 1**rows**(if N is odd)**or**N/2**rows**(if N is even)**. - The number of
**‘*’**in the first row is 1 and it increases by 2 as we move down the rows. - If
**i**is the number of**‘*’**we need to print in the current row. Then the number of spaces before**‘*’**is**( N – i ) / 2**. - Print
**(N – i)/2**spaces then Print**i ‘*’**characters for each row. - After printing all rows of the Upper Triangle, we move to the lower part.
- The Lower Triangle consists of
**N/2**rows. - The first row of Lower Triangle consists of
**N/2 + N/2 – 1****‘*’**characters.**Reason:****N/2 + N/2 – 1**gives the closest odd number to**N**that is less than**N**.

For example, if**N**is**6**then**N/2 + N/2 – 1 = 5**.

If**N**is**5**, then**N/2 + N/2 – 1 = 3**. - The number of
**‘*’**decreases by 2 as we move down the rows. - If
**i**is the number of**‘*’**we need to print in the current row. Then the number of spaces before**‘*’**is**( N – i ) / 2**. - Print
**(N – i)/2**spaces then Print**i ‘*’**characters for each row.

The Program can be implemented in 2 ways

- Diamond Pattern Program using a loop (iteratively)
- Diamond Pattern Program using recursion (recursively)

### Program to Print Diamond Pattern in C using Loop

```
#include<stdio.h>
int main() {
int n, i, k;
printf("Enter Number: ");
scanf("%d", &n);
// Printing the upper part of Diamond Pattern
// i is the number of stars we print in
// each iteration of loop
for (int i = 1; i <= n; i = i + 2)
{
// printing (n-i)/2 spaces
for (k = 1; k <= (n - i) / 2; ++k)
{
printf(" ");
}
// printing i '*"
for (k = 1; k <= i; ++k)
{
printf("*");
}
printf("\n");
}
// Printing Lower part of Diamond Pattern
// n/2+n/2-1 converts integer n to nearest odd number lower than n
// this is done because the number of stars in each row is odd
// i is the number of stars we print in
// each iteration of loop
for (i = n / 2 + n / 2 - 1; i >= 1; i = i - 2)
{
// printing (n-i)/2 spaces
for (k = 1; k <= (n - i) / 2; ++k)
{
printf(" ");
}
// printing i '*"
for (k = 1; k <= i; ++k)
{
printf("*");
}
printf("\n");
}
return 0;
}
```

#### Output

### Program to Print Diamond Pattern in C using recursion

```
#include<stdio.h>
// i is the number of '*' we need to print
// del is used to increment/decrement i
// for next recursive call
void print(int n, int i, int del) {
int k;
if (i<1 || i>n)
{
return;
}
// printing (n-i)/2 spaces
for (k = 1; k <= (n - i) / 2; ++k)
{
printf(" ");
}
// printing i '*"
for (k = 1; k <= i; ++k)
{
printf("*");
}
printf("\n");
print(n, i + del, del);
}
int main() {
int n, i, k;
printf("Enter Number: ");
scanf("%d", &n);
// Printing Upper Triangle
print(n, 1, 2);
// Printing Lower Triangle
// n/2 + n/2 - 1 convert n to closest odd number
// which is lesser than n
print(n, n / 2 + n / 2 - 1, -2);
return 0;
}
```

#### Output

*What to study next?*

** Leave Comment if you face any problem**.