Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c
Diamond pattern program in Java language

C program to Diamond pattern program

Posted on January 9, 2020January 9, 2020

Table of Contents

  • C program to Diamond pattern program
    • C code to the Diamond pattern using for loop
    • C code to Diamond using while loop
    • C code to Diamond  using the do-while loop
    • Related

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

C program to Diamond pattern program
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

C program to Diamond pattern program
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

C program to Diamond pattern program
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

 

 

 

Related

Recent Posts

  • Multiply two numbers in Java using scanner| 5 different ways
  • 5 different ways to Divide two numbers in Java using scanner
  • Learn 8 Ways to Subtract Two Numbers Using Methods in Java
  • 10 ways to subtract two numbers in Java
  • Java Code Examples – Multiply Two Numbers in 5 Easy Ways
  • How to Divide two numbers in Java| 5 different ways

tag

Addition (8) Array (38) C++ language (91) C language (98) c sharp (23) Division (8) Function (29) if else (32) Java language (108) JavaScript (5) loops (138) Multiply (8) Oop (2) patterns (66) PHP (13) Python Language (38) Subtraction (9) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2026 Code for Java c | Powered by SuperbThemes