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 for the diamond pattern using loops

Posted on January 9, 2020February 7, 2020

Table of Contents

  • C++ program for the diamond pattern using loops
    • program for the diamond pattern
      • Program for the diamond pattern using for loop
      • Program for the diamond pattern using while loop
      • Program for the diamond pattern using do-while loop

C++ program for the diamond pattern using loops

In this article, we will discuss the C++ program for the diamond pattern using loops

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

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

program for the diamond pattern

Program for 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 <iostream>
#include <conio.h>
using namespace std;

int main()
{
    char ch;
    int rows,i,j,Num_Of_space=1;
    cout<<"Enter the number of rows: ";
    cin>>rows;
    cout<<"Enter the symbol: ";
    cin>>ch;

    Num_Of_space=rows-1;

    for(i=1; i<=rows; i++){//outer for loop iterates through rows
        for(j=1; j<=Num_Of_space; j++)
            cout<<" ";//print space - inner for loop
            Num_Of_space--;

        for(j=1; j<=2*i-1; j++)
            cout<<ch;//print character

        cout<<"\n";//move to next line

    }

    Num_Of_space=1;
    for(i=1; i<=rows-1; i++){
            for(j=1; j<=Num_Of_space; j++)
            cout<<" ";//print space - inner for loop
    Num_Of_space++;

        for(j=1; j<=2*(rows-i)-1; j++)
            cout<<ch;//print charactor
            cout<<"\n";//mobe to next line

        }
        getch();

    return 0;
}

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

 

Program for the diamond pattern 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 <iostream>
#include <conio.h>
using namespace std;

int main()
{
    char ch;
    int rows,i,j,Num_Of_space=1;
    cout<<"Enter the number of rows: ";
    cin>>rows;//takes input from the user for rows
    cout<<"Enter the symbol: ";
    cin>>ch;//Takes symbol from the user for pattern

    Num_Of_space=rows-1;
i=1;
    while(i<=rows){//outer while loop iterates through rows
            j=1;
        while(j<=Num_Of_space){
            cout<<" ";//print space - inner while loop
             j++;
        }
            Num_Of_space--;
j=1;
        while(j<=2*i-1){
            cout<<ch;//print character  -inner while loop
            j++;
        }
        cout<<"\n";
 i++;
    }

    Num_Of_space=1;
    i=1;
    while(i<=rows-1){
            j=1;
            while(j<=Num_Of_space){
            cout<<" ";//print space - inner while loop
     j++;
            }
    Num_Of_space++;
j=1;
        while(j<=2*(rows-i)-1){
            cout<<ch;
             j++;
        }
            cout<<"\n";
 i++;
        }
        getch();

    return 0;
}

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

Program for the diamond pattern using 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 <iostream>
#include <conio.h>
using namespace std;

int main()
{
    char ch;
    int rows,i,j,Num_Of_space=1;
    cout<<"Enter the number of rows: ";
    cin>>rows;//Takes input from the user for rows
    cout<<"Enter the symbol: ";
    cin>>ch;//Takes input from the user for pattern

    Num_Of_space=rows-1;
i=1;
    do{//outer do while loop  - print upper part
            j=1;
       do{//inner do while loop 1
            cout<<" ";//print space
             j++;
        } while(j<=Num_Of_space);
            Num_Of_space--;
j=1;
        do{//inner do while loop 2
            cout<<ch;//print given character
            j++;
        }while(j<=2*i-1);
        cout<<"\n";//move to next line
 i++;
    }while(i<rows);

    Num_Of_space=1;
    i=1;
    do{//outer do while loop - print inner part
            j=1;
            do{//inner do while loop 1
            cout<<" ";//print space
     j++;
            }while(j<=Num_Of_space);
    Num_Of_space++;
j=1;
        do{//inner do while loop 2
            cout<<ch;//print given character
             j++;
        }while(j<=2*(rows-i)-1);
            cout<<"\n";//move to next line
 i++;
        }while(i<=rows-1);
        getch();

    return 0;
}

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

 

Suggested for you

for loop in C++ language

while loop in C++ language

do-while loop in C++ language

 

Similar post

pyramid pattern program in C- using loops

Java pyramid pattern program – using loops

C++ star pyramid pattern  – using loops

C++ code to Inverted Pyramid pattern

Java code to Inverted Pyramid pattern

C code to Inverted Pyramid pattern

C language Hollow Pyramid pattern

Java language Hollow Pyramid pattern

Display Hollow Inverted Pyramid pattern in C language

Display Hollow Inverted Pyramid pattern in Java language

Display diamond(empty) star pattern in C language

Display diamond(empty) star pattern in Java language

Display diamond shape in Java language

Display diamond shape in C language

 

 

 

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes