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

Multiply two numbers in Java using scanner| 5 different ways

Multiply two numbers in Java using scanner| 5 different ways In this article, we will…

3 months ago

5 different ways to Divide two numbers in Java using scanner

5 Different ways to Divide two numbers in Java using scanner In this article, we…

3 months ago

Learn 8 Ways to Subtract Two Numbers Using Methods in Java

Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…

4 months ago

10 ways to subtract two numbers in Java

10 ways to subtract two numbers in Java In this article, we will discuss the…

4 months ago

Java Code Examples – Multiply Two Numbers in 5 Easy Ways

Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…

4 months ago

How to Divide two numbers in Java| 5 different ways

How to Divide two numbers in Java| 5 different ways In this article, we will…

4 months ago

This website uses cookies.