# Half Diamond Pattern in C

There are many pattern programs are written by programmers for practice purposes. The diamond pattern programs are one of them. Here we will write half diamond pattern in the C programming language.

## C Program to Print Half Diamond Star Pattern

Write a C program to print the half diamond using stars. If the input n=5, the output is:-

``````*
**
***
****
*****
****
***
**
*``````

For input n, there are `2*n-1` rows. For row (say i) if `i<n` then rows having `i` stars, else it has `-(2*n-i)` rows.

In the below program, the star variable holds the number of stars in the nth row. In every row, the star variable is updated. When `i>n` then (2*n-i) will be negative so, the abs() function is used to find the absolute value of (2*n-i).

``````#include<stdio.h>
#include<stdlib.h>
int main()
{
int n, star;

printf("Enter number of rows: ");
scanf("%d",&n);

for(int i=1; i< 2*n; i++)
{
if(i < n) star=i;
else star = abs(2*n-i);

for(int j=1; j<=star; j++)
printf("*");

printf("\n"); //next line
}
return 0;
}``````

## C Program to Print Mirrored Half Diamond Star Pattern

Write a C program to display mirrored half diamond using the stars. For input n=5 the output is,

``````    *
**
***
****
*****
****
***
**
*``````

For input n there are `2*n-1` rows. In the ith row, there are `n-i` (positive value) spaces, after that there are` i `stars are present.

Program to display mirrored half diamond star pattern using the C programming language.

``````#include<stdio.h>
#include<stdlib.h>
int main()
{
int n;

printf("Enter number of rows: ");
scanf("%d",&n);

for(int i=1; i<= 2*n; i++)
{
for(int j=1; j<=n; j++)
if(j<= abs(n-i)) printf(" ");
else printf("*");

printf("\n"); //next line
}

return 0;
}``````

## Half Diamond Pattern Programs Using Numbers

Write a C program to display the given below half diamond using numbers. For input n = 4 the output is,

``````1
2*2
3*3*3
4*4*4*4
3*3*3
2*2
1``````

For input n there are `2*n-1` rows are there. In ith row up to `2*i-1` places, on every even place, there is a star and on odd places the value of i is present.

``````#include<stdio.h>
#include<stdlib.h>
int main()
{
int n, place;

printf("Enter number of rows: ");
scanf("%d",&n);

for(int i=1; i< 2*n; i++)
{
if(i < n) place=i;
else place = abs(2*n-i);

for(int j=1; j< 2*place; j++)
if(j%2==0) printf("*"); //stars
else printf("%d",place); //numbers

printf("\n"); //next line
}

return 0;
}``````

Write a C program for the given below number pattern. The input n=5, the output is,

``````1
12
123
1234
12345
1234
123
12
1``````

Code for the above pattern,

``````#include<stdio.h>
#include<stdlib.h>
int main()
{
int n, place, a;

printf("Enter number of rows: ");
scanf("%d",&n);

for(int i=1; i< 2*n; i++)
{
a=1;

if(i < n) place=i;
else place = abs(2*n-i);

for(int j=1; j<=place; j++)
printf("%d",a++);

printf("\n"); //next line
}

return 0;
}``````

## Similar Pattern

Write a C program for the given below pattern. For the input n=5, the output is:-

``````1
123
12345
1234567
123456789
1234567
12345
123
1``````

Code for the above pattern,

``````#include<stdio.h>
#include<stdlib.h>
int main()
{
int n, place, a;

printf("Enter number of rows: ");
scanf("%d",&n);

for(int i=1; i< 2*n; i++)
{
a=1;

if(i < n) place=i;
else place = abs(2*n-i);

for(int j=1; j<=2*place-1; j++)
printf("%d",a++);

printf("\n"); // next line
}

return 0;
}``````

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!