➤ If-else Statement in C
➤ Programs on if-else
➤ Switch Case in C
➤ Switch case Programs
➤ Conditional Operator
➤ While loop in C
➤ Do-while loop in C
➤ While vs do-while
➤ For loop in C
➤ Break keyword in C
➤ Continue keyword in C
➤ Break vs Exit in C
➤ Goto keyword in C
☕️ Flow Control Programs
➤ Largest in 3 Numbers
➤ Find Grade of student
➤ Find the absolute value
➤ Vowel or Consonant
➤ Leap Year Program
➤ Simple calculator in C
➤ Check Odd or Even
➤ Roots of Quadratic Equation
➤ Find Reverse of Number
➤ Factors of a number in C
➤ Generate Multiplication table
➤ Find Power of a Number
➤ Find GCD and LCM
➤ Find factorial of Number
➤ Count Number of Digits
➤ Sum of digits in Number
➤ Sum of N Natural Numbers
➤ Sum of Squares of Natural No.
➤ Find Sum of Odd Numbers
➤ Find the Sum of Series
➤ Find Fibonacci series in C
➤ Sum of the Fibonacci series
➤ Sum until enters +ve numbers
➤ Sum of max 10 no. & Skip -ve
☕️ C Conversion Programs
➤ Celsius to Fahrenheit
➤ Fahrenheit to Celsius
➤ Decimal ↔ Binary
➤ Decimal ↔ Octal
➤ Octal ↔ Binary in C
☕️ Number Programs in C
➤ Prime Number in C
➤ Strong Number in C
➤ Krishnamurthy Number
➤ Neon Number in C
➤ Palindrome number
➤ Perfect Number in C
➤ Armstrong Number
☕️ Pattern Programs in C
➤ Pattern programs in C
➤ Printing pattern using loops
➤ Floyd’s triangle Program
➤ Pascal Triangle Program
➤ Pyramid Star Pattern in C
➤ Diamond Pattern Programs
➤ Half Diamond pattern in C
➤ Print Diamond Pattern
➤ Hollow Diamond Pattern
➤ Diamond Pattern of Numbers
There are many pattern programs are written by programmers for practice purposes. The diamond pattern programs are one of them. Here we will write C program to print a diamond pattern.
C Program to Print Diamond Star Pattern
For a given number n the diamond shape will be of 2*n rows.
*
***
*****
*******
*********
*******
*****
***
*
C program for the above pattern,
#include<stdio.h>
int main()
{
int n;
printf("Enter number of rows: ");
scanf("%d",&n);
for(int i=1; i<=n; i++)
{
for(int j=i; j<=n; j++)
{
printf(" ");
}
for(int k=1; k<=2*i-1; k++)
{
printf("*");
}
printf("\n");
}
for(int i=n-1; i>=1; i--)
{
for(int j=n; j>=i; j--)
{
printf(" ");
}
for(int k=1; k<=2*i-1; k++)
{
printf("*");
}
printf("\n");
}
return 0;
}
Output:-
Enter number of rows: 3
*
***
*****
***
*
Enter number of rows: 5
*
***
*****
*******
*********
*******
*****
***
*
Print the diamond pattern using recursion
We can display the same pattern using recursion also. The pattern is divided into two portions. The increasing portion and the decreasing portion of the pattern. The below program displays the diamond pattern of stars using recursion.
#include<stdio.h>
void printDiamond (int r)
{
int c, space;
static int stars = -1;
if (r <= 0) return;
// increasing portion of the pattern
space = r - 1;
stars += 2;
for (c = 0; c < space; c++)
printf(" ");
for (c = 0; c < stars; c++)
printf("*");
printf("\n"); //next line
printDiamond(--r);
// decreasing portion of the pattern
space = r + 1;
stars -= 2;
for (c = 0; c < space; c++)
printf(" ");
for (c = 0; c < stars; c++)
printf("*");
printf("\n"); //next line
}
int main ()
{
int n;
printf("Enter number of rows: ");
scanf("%d",&n);
printDiamond(n);
return 0;
}
Program display first row of the pattern and then due to recursive function second row is displayed, and so. After displaying all increasing portions of the pattern, the value of r becomes 0. In the next recursive call the recursive function return because of r<=0. Now, the remaining codes in each call will be executed.
Note that for n=5 the recursive function called only 5 times not 10 times.
C program to print diamond star pattern at the center of the screen
Generally, on a computer screen, we can print Maximum 80 characters horizontally. The center is half of the row. So in every row, there should be 40-i spaces, here i
is the row. Remaining all other codes will be space as the previous one.
#include<stdio.h>
int main()
{
int n;
printf("Enter number of rows: ");
scanf("%d",&n);
for(int i=1; i<=n; i++)
{
for(int j=1; j<=40-i; j++)
{
printf(" ");
}
for(int k=1; k<=2*i-1; k++)
{
printf("*");
}
printf("\n");
}
for(int i=n-1; i>=1; i--)
{
for(int j=40; j>i; j--)
{
printf(" ");
}
for(int k=1; k<=2*i-1; k++)
{
printf("*");
}
printf("\n");
}
return 0;
}
Output:-
Enter number of rows: 5
*
***
*****
*******
*********
*******
*****
***
*

C Program to Print Inverse Diamond Pattern
Program to print Inverse Diamond pattern, for input n=5 the output is:-
**********
**** ****
*** ***
** **
* *
** **
*** ***
**** ****
**********
C Program for the above pattern,
#include<stdio.h>
int main()
{
int n;
printf("Enter value of n: ");
scanf("%d",&n);
for(int i=1; i<=n; i++)
{
for(int j=i; j<=n; j++)
{
printf("*");
}
for(int k=1; k<2*i-1; k++)
{
printf(" ");
}
for(int j=i; j<=n; j++)
{
printf("*");
}
printf("\n");
}
for(int i=n-1; i>=1; i--)
{
for(int j=n; j>=i; j--)
{
printf("*");
}
for(int k=1; k<2*i-1; k++)
{
printf(" ");
}
for(int j=n; j>=i; j--)
{
printf("*");
}
printf("\n");
}
return 0;
}
Output:-
Enter value of n: 5
**********
**** ****
*** ***
** **
* *
** **
*** ***
**** ****
**********
Enter value of n: 7
**************
****** ******
***** *****
**** ****
*** ***
** **
* *
** **
*** ***
**** ****
***** *****
****** ******
**************
Diamond Pattern of Numbers
For input n=5 the output is,
1
123
12345
1234567
123456789
1234567
12345
123
1
C Program for the above pattern,
#include<stdio.h>
int main()
{
int n, a;
printf("Enter number of rows: ");
scanf("%d",&n);
for(int i=1; i<=n; i++)
{
a=1;
for(int j=i; j<=n; j++)
{
printf(" ");
}
for(int k=1; k<=2*i-1; k++)
{
printf("%d",a++);
}
printf("\n");
}
for(int i=n-1; i>=1; i--)
{
a=1;
for(int j=n; j>=i; j--)
{
printf(" ");
}
for(int k=1; k<=2*i-1; k++)
{
printf("%d",a++);
}
printf("\n");
}
return 0;
}
Output for the different input values:-
Enter number of rows: 3
1
123
12345
123
1
Enter number of rows: 5
1
123
12345
1234567
123456789
1234567
12345
123
1
Display the given diamond pattern of numbers. For n value of 4, the output is:-
0
010
01210
0123210
012343210
0123210
01210
010
0
In this pattern, each row starts with zero and also ends with zero. It is a diamond pattern so every row contains odd items. This pattern is the same as the previous one but it has different numbers.
#include<stdio.h>
int main()
{
int n, a=0;
printf("Enter value of n: ");
scanf("%d",&n);
// increasing portion of the pattern
for(int i=1; i<=n+1; i++)
{
for(int j=i; j<=n; j++)
{
printf(" "); //space
}
for(int k=1; k<=2*i-1; k++)
{
if(k<i) printf("%d",a++);
else if(k==i) printf("%d",a);
else printf("%d",--a);
}
printf("\n");
}
// decreasing portion of the pattern
for(int i=n; i>=1; i--)
{
for(int j=n; j>=i; j--)
{ printf(" ");
}
for(int k=1; k<=2*i-1; k++)
{
if(k<i) printf("%d",a++);
else if(k==i) printf("%d",a);
else printf("%d",--a);
}
printf("\n");
}
return 0;
}
Output for the different input values:-
Enter value of n: 3
0
010
01210
0123210
01210
010
0
Enter value of n: 5
0
010
01210
0123210
012343210
01234543210
012343210
0123210
01210
010
0
There are many other diamond pattern like Half Diamond pattern in C, Hollow Diamond Pattern in C, and Diamond Pattern of Numbers.
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!