Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C++ Exercises: Display the pattern like a diamond

C++ For Loop: Exercise-44 with Solution

Write a program in C++ to display the 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:

Flowchart: Display the pattern like a diamond

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?