C Language

C program to Diamond pattern program

C program to Diamond pattern program

In this article, we will discuss the programming concept of C program to Diamond star pattern program

we can create star number, alphabet, binary patterns using loops (for, while and do-while loop) in C language

In this post, we are going to learn how to create a diamond pattern in C language using the given symbol

 

C code to the Diamond pattern using for loop

This program allows the user to enter the number of rows and the symbol then the program displays the diamond pattern with the given symbol using for loop in C language

Program 1

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

int main()
{
    int rows,i,j,Num_Of_space=1;
    char ch;
    printf("Enter number of rows\n");
    scanf("%d%c",&rows,&ch);
    printf("Enter the symbol as you wish:");
    scanf("%c",&ch);
    Num_Of_space=rows-1;

    for(i=1; i<=rows; i++){
        for(j=1; j<=Num_Of_space; j++)
            printf(" ");
            Num_Of_space--;

        for(j=1; j<=2*i-1; j++)
            printf("%c",ch);

        printf("\n");

    }

    Num_Of_space=1;
    for(i=1; i<=rows-1; i++){
            for(j=1; j<=Num_Of_space; j++)
            printf(" ");
    Num_Of_space++;

        for(j=1; j<=2*(rows-i)-1; j++)
            printf("%c",ch);
            printf("\n");

        }
        getch();

    return 0;
}

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

Pattern 1

 

C code to Diamond using while loop

This program allows the user to enter the number of rows and the symbol then the program displays the diamond pattern with the given symbol using while loop in C language

Program 2

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

int main()
{
    int rows,i,j,Num_Of_space=1;
    char ch;
    printf("Enter number of rows\n");
    scanf("%d%c",&rows,&ch);
    printf("Enter the symbol as you wish:");
    scanf("%c",&ch);
    Num_Of_space=rows-1;
i=1;
    while(i<=rows){
            j=1;
        while(j<=Num_Of_space){
            printf(" ");
     j++;
        }
            Num_Of_space--;
j=1;
        while(j<=2*i-1){
            printf("%c",ch);
             j++;
        }
        printf("\n");
 i++;
    }

    Num_Of_space=1;
      i=1;
    while(i<=rows-1){
j=1;
            while(j<=Num_Of_space){
            printf(" ");
             j++;
            }
    Num_Of_space++;
j=1;
        while( j<=2*(rows-i)-1){
            printf("%c",ch);
            j++;
        }
            printf("\n");

 i++;
        }
        getch();

    return 0;
}

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

Pattern 2

 

C code to Diamond  using the do-while loop

This program allows the user to enter the number of rows and the symbol then the program displays the diamond pattern with the given symbol using the do-while loop in C language

Program 3

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

int main()
{
    int rows,i,j,Num_Of_space=1;
    char ch;
    printf("Enter number of rows\n");
    scanf("%d%c",&rows,&ch);
    printf("Enter the symbol as you wish:");
    scanf("%c",&ch);
    Num_Of_space=rows-1;
i=1;
    do{
            j=1;
        do{
            printf(" ");
     j++;
        }while(j<=Num_Of_space);
            Num_Of_space--;
j=1;
       do{
            printf("%c",ch);
             j++;
        } while(j<=2*i-1);
        printf("\n");
 i++;
    }while(i<rows);

    Num_Of_space=1;
      i=1;
    do{
j=1;
            do{
            printf(" ");
             j++;
            }while(j<=Num_Of_space);
    Num_Of_space++;
j=1;
        do{
            printf("%c",ch);
            j++;
        }while( j<=2*(rows-i)-1);
            printf("\n");

 i++;
        }while(i<rows);
        getch();

    return 0;
}

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

Pattern 3

Suggested post you

For loop in C language

While loop in C language

Do-while loop in C language

Nested for loop in C language

Nested while loop in C language

Nested do while loop in C language

 

Similar post

C++ pyramid star pattern  – using loops

Java pyramid star pattern – using loops

C pyramid star pattern – using loops

C++ program to Inverted Pyramid star pattern

Java program to Inverted Pyramid star pattern

C++ code to Hollow Pyramid star pattern

Java code to Hollow Pyramid star pattern

Print Hollow Inverted Pyramid pattern in C++ language

Print Hollow Inverted Pyramid pattern in Java language

Java Hollow diamond star pattern

C++ Hollow diamond star pattern

Display diamond shape in Java language

Display diamond shape in C language

Display diamond shape in C++ language

 

 

 

Karmehavannan

Recent Posts

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

4 weeks ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

4 weeks ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.