Diamond Pattern Programs in C

Flow Control
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 in C are one of them. Here we will write Half diamond pattern programs in C, Full diamond pattern programs in C, Hollow diamond pattern programs, and different diamond pattern programs using numbers in C.

Half Diamond Pattern Programs in C

1. Write a C program to print half diamond of stars.

The half diamond pattern programs contain only half portion of the diamond.

 *
 **
 ***
 ****
 ***** 
 ****
 ***
 **
 *

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++)
  {
    // condition for number
    // of stars in a row
    if(i < n) star=i;
    else star = abs(2*n-i);

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

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

  return 0;
}

2. Write a C program to print mirrored half diamond of stars.

    *
   **
  ***
 ****
*****
 ****
  ***
   **
    *

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

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

  printf("Enter N value: ");
  scanf("%d", &n);

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

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

  return 0;
}

3. Write a C program to print half diamond of numbers and stars.

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

The code for the above pattern,

#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;
}

4. Write a C program to print half diamond of numbers.

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);
    
    // print digit
    for(int j=1; j<=place; j++)
    printf("%d",a++);

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

  return 0;
}

5. Write a C program to print half diamond of numbers.

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;
}

Full Diamond Pattern

6. Write a C program to print full diamond of stars.

     *
    ***
   *****
  *******
 *********
  *******
   *****
    ***
     *

The code for the above pattern is given below.

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

   printf("Enter N value: ");
   scanf("%d",&n);

   for(int i=1; i<=n; i++)
   {
     // print space
     for(int j=i; j<=n; j++)
     {
       printf(" ");
     }
     // print star
     for(int k=1; k<=2*i-1; k++)
     {
       printf("*");
     }
     // new line
     printf("\n");
   }

   for(int i=n-1; i>=1; i--)
   {
     // print space
     for(int j=n; j>=i; j--)
     {
       printf(" ");
     }
     // print star
     for(int k=1; k<=2*i-1; k++)
     {
       printf("*");
     }
     // new line
     printf("\n");
   }

   return 0;
}

7. Write a C program to print full diamond of spaces and stars.

**********
****  ****
***    ***
**      **
*        *
**      **
***    ***
****  ****
**********

The code for the above pattern is given below,

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

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

   // for first half portion
// from top to bottom
for(int i=1; i <= n; i++)
{
// print star
for(int j=i; j <= n; j++)
{
printf("*");
}
// print space
for(int k=1; k < 2*i-1; k++)
{
printf(" ");
}
// print star
for(int l=i; l <= n; l++)
{
printf("*");
}
// print new line
printf("\n");
} // for second half portion
for(int i=n-1; i >= 1; i--)
{
// print star
for(int j=n; j >= i; j--)
{
printf("*");
}
// print space
for(int k=1; k < 2*i-1; k++)
{
printf(" ");
}
// print star
for(int l=n; l >= i; l--)
{
printf("*");
}
// print new line
printf("\n");
} return 0; }

8. Write a C program to print full diamond of numbers.

     1
    123
   12345
  1234567
 123456789
  1234567
   12345
    123
     1

The code for the above pattern is given below.

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

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

   // for first half portion 
   // from top to bottom
   for(int i=1; i<=n; i++)
   {
     // In each iteration a will
     // start from 1
     a = 1;

     // print space
     for(int j=i; j <= n; j++)
     {
       printf(" ");
     }

     // print digit
     for(int k=1; k <= 2*i-1; k++)
     {
       printf("%d",a++);
     }

     // new line
     printf("n");
   }

   // for second portion 
   for(int i=n-1; i>=1; i--)
   {
     // In each iteration a will
     // start from 1
     a=1;

     // print space
     for(int j=n; j>=i; j--)
     {
       printf(" ");
     }

     // print digit
     for(int k=1; k<=2*i-1; k++)
     {
       printf("%d",a++);
     }

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

   return 0;
}

9. Write a C program to print full diamond of numbers, starting with zero and ending with zero.

    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.

#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++)
   {

     // print space
     for(int j=i; j<=n; j++)
     {
       printf(" ");
     }

     // print digit
     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);
      }

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

   // decreasing portion of the pattern
   for(int i=n; i>=1; i--)
   {
     // print space
     for(int j=n; j>=i; j--)
     {
       printf(" ");
     }

     // print digit
     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);
     }

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

   return 0;
}

Hollow Diamond Pattern in C

10. Write a C program to print hollow diamond of stars.

     *
    * *
   *   *
  *     *
 *       *
  *     *
   *   *
    * *
     *

The code for the above pattern is,

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

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

   // for first half portion
   for(int i=1; i<=n; i++)
   {
     // print space
     for(int j=i; j<=n; j++)
     {
       printf(" ");
     }

     // print digit or space
     for(int k=1; k<=2*i-1; k++)
     {
       if(k==1 || k==(2*i-1)) printf("*");
       else printf(" ");
     }

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

   // for second half portion
   for(int i=n-1; i>=1; i--)
   {
     // print space
     for(int j=n; j>=i; j--)
     {
       printf(" ");
     }

     // print digit or space
     for(int k=1; k<=2*i-1; k++)
     {
       if(k==1 || k==2*i-1) printf("*");
       else printf(" ");
     }

     // new line
     printf("\n");

   }

   return 0;
}

11. Write a C program to print hollow diamond of numbers.

     1
    2 2
   3   3
  4     4
 5       5
  4     4
   3   3
    2 2
     1

The pattern starts with 1 and displays up to 5. The first and last row contains only 1 and the remaining rows contain exactly 2 same numbers. From 1st to 5th row the size of pattern increases and from 6th to 9th row the size of pattern decreased.

#include<stdio.h>
int main()
{
   int n, a=1;

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

   // for first half portion
   for(int i=1; i<=n; i++)
   {

     // print space
     for(int j=i; j<=n; j++)
     {
       printf(" ");
     }

     // print digit and space
     for(int k=1; k<=2*i-1; k++)
     {
       if(k==1 || k==2*i-1) printf("%d",a);
       else printf(" ");
     }

     // decrease temp variable
     a++;

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

   // update temp variable
   a=n-1;

   // for second half portion
   for(int i=n-1; i>=1; i--)
   {

     // print space
     for(int j=n; j>=i; j--)
     {
       printf(" ");
     }

     // print digit and space
     for(int k=1; k<=2*i-1; k++)
     {
       if(k==1 || k==2*i-1) printf("%d",a);
       else printf(" ");
     }

     // decrease temp variable
     a--;

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

   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!

Flow Control
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 among 3 Numbers
Find Grade of student
Find the absolute value
Check 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

Leave a Comment

Your email address will not be published. Required fields are marked *