C++ Exercises: Display the pattern like a diamond
C++ For Loop: Exercise-44 with Solution
Write a C++ program to display a pattern like a diamond.
Sample Solution:
C++ Code :
#include <iostream>
using namespace std;
int main()
{
int i,j,r;
cout << "\n\n Display the pattern like a diamond:\n";
cout << "----------------------------------------\n";
cout << " Input number of rows (half of the diamond): ";
cin >> r;
for(i=0;i<=r;i++)
{
for(j=1;j<=r-i;j++)
cout<<" ";
for(j=1;j<=2*i-1;j++)
cout<<"*";
cout<<endl;
}
for(i=r-1;i>=1;i--)
{
for(j=1;j<=r-i;j++)
cout<<" ";
for(j=1;j<=2*i-1;j++)
cout<<"*";
cout<<endl;;
}
}
Sample Output:
Display the pattern like a diamond: ---------------------------------------- Input number of rows (half of the diamond): 5 * *** ***** ******* ********* ******* ***** *** *
Flowchart:

C++ Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a program in C++ to print the Floyd's Triangle.
Next: Write a program in C++ to display Pascal's triangle like pyramid.
What is the difficulty level of this exercise?
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join