Recent

Comments

Follow us on Facebook

Sunday 12 November 2017

Output Diamond Pattern in C++

Output  Diamond Pattern in C++

To print diamond pattern of stars in C++ programming, you have to ask from the user to enter the number of rows (upto which he/she want to print the diamond patter) for dimension of diamond. Now to print diamond pattern of stars, use six for loops. The first for loop (outer for loop which contains two for loops. One to print spaces and the second to print stars) is to print pyramid pattern of stars. The second for loop (the second outer for loop which also contains two for loops. One to print spaces and second to print stars) is used to print reverse pyramid of stars. Which wholly makes a diamond pattern of stars as shown here in the following program.

C++ Programming Code to Output Diamond Pattern

Following C++ program ask to the user to enter number of rows for diamond dimension to print the diamond pattern, then display the result on the screen:
/* C++ Program - Print Diamond Pattern */
  
#include<iostream.h>
#include<conio.h>
void main()
{
    clrscr();
    int n, c, k, space=1;
    cout<<"Enter number of rows (for diamond dimension) : ";
    cin>>n;
    space=n-1;
    for (k=1; k<=n; k++)
    {
 for(c=1; c<=space; c++)
 {
  cout<<" ";
 }
 space--;
 for(c=1; c<=(2*k-1); c++)
 {
  cout<<"*";
 }
 cout<<"\n";
    }
    space=1;
    for(k=1; k<=(n-1); k++)
    {
 for(c=1; c<=space; c++)
 {
  cout<<" ";
 }
 space++;
 for(c=1 ; c<=(2*(n-k)-1); c++)
 {
  cout<<" ";
 }
 cout<<"\n";
    }
    getch();
}
When the above C++ program is compile and executed, it will produce the following result:
C++ program print diamond pattern

0 on: "Output Diamond Pattern in C++"

Write a program, that takes 16-Elements in Two-Dimensional-Array that has 4 rows & 4 columns, then display the diagonal of Matrix.

Write a program, that takes 16-Elements in Two-Dimensional-Array that has 4 rows & 4 columns, then display the diagonal of Matrix. ...

propeller ads

PropellerAds