Diamond pattern

C code to display Diamond number pattern

C code to display Diamond number pattern

In this tutorial, we will discuss a concept of the C code  to display Diamond number pattern

In this post, we will learn how to create the diamond number pattern using loops in C language.

We can use for loop, while loop or do while loop to display different diamond number patterns in C programming language.

C code to display Diamond number pattern

Here, we will use for loop to print different diamond number patterns

Program 1

Diamond  pattern 1

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int rows;
    printf("Enter the number of rows: ");
    scanf("%d",&rows);
    int row=1,i,j,k;

for(i=rows/2; i>0; i--){    //print upper part
    for(j=1; j<=i; j++){
      printf(" ");//print space
}
for(j=1; j<=row; j++){
      printf("%d",row);
      printf(" ");
}
printf("\n");//move to next line
row++;
}

for(i=0; i<=rows/2; i++){
  for(j=1; j<=i; j++){
      printf(" ");//print space
}

for(j=row; j>=1; j--){  //print lower part
      printf("%d",row);
      printf(" ");
}
printf("\n");//move to next line
row--;
        }
    return 0;
}


When the above code is executed, it produces the following result

Enter the number of rows: 8
        1
      2   2
    3   3  3
  4   4   4  4
5   5   5   5  5
  4   4   4  4
    3   3   3
      2   2
        1

 

Program 2

Diamond  pattern 2

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i,j,k,l,rows;
    printf("|Enter the number of rows");
    scanf("%d",&rows);
    for(i=1; i<=rows; i++){    //print upper part
    for(j=1; j<=rows-i; j++){
      printf(" ");//print initial space for upper part
}
for(k=i; k>=1; k--){
      printf("%d",k);
}
for(l=2; l<=i; l++){
      printf("%d",l);
}
printf("\n");//move to next line
}

for(i=rows-1; i>=1; i--){  //print lower part
    for(j=0; j<=rows-1-i; j++){
     printf(" ");//print initial space for lower part
}
for(k=i; k>=1; k--){
      printf("%d",k);
}
for(l=2; l<=i; l++){
      printf("%d",l);
}
printf("\n");//move to next line
        }
    return 0;
}

 

When the above code is executed, it produces the following result

Enter the number of rows: 6
          1
        212
      32123
    4321234
  543212345
65432123456
  543212345
    4321234
      32123
        212
          1

 

Program 3

Diamond  pattern 3

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int rows;
    printf("Enter the number of rows: ");
    scanf("%d",&rows);
    int row=1,i,j,k;

for(i=1; i<=rows; i++){    //print upper part
    for(j=1; j<=rows-i; j++){
      printf(" ");
}
for(k=1; k<=i; k++){
      printf("%d",k);
      printf(" ");
}
printf("\n");//move to next line
}

for(i=1; i<=rows-1; i++){  //print lower part
    for(j=1; j<=i; j++){
      printf(" ");
}
for(k=1; k<=rows-i; k++){
      printf("%d",k);
      printf(" ");
}
printf("\n");//move to next line
        }
    }

When the above code is executed, it produces the following result

Enter the number of rows: 6
         1
       1  2
     1  2  3
   1  2  3  4
  1  2  3  4  5
1  2  3  4  5  6
  1  2  3  4  5
    1  2  3  4
      1  2  3 
        1  2
          1

 

 

Simillarpost

Diamond number pattern in C++ language

Diamond number pattern in Java language

 

Suggested for you

input-output function in C

Operator in C language

Data type in C  language

variable in C  language

For loop in C language

Nested for loop in C language

 

 

 

 

 

 

Diamond number pattern in C++ language
Diamond number pattern in Java language
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago