Table of Contents
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
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