

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program to print the diamond shape
This is a C++ Program to print the diamond shape.
Algorithm
Begin Take the no of rows n means the dimension of the diamond shape as input. Declare the variables i, j and initialize space=1. Initialize space = n-1. Run for loop till n. Run for loop to print space. Decrease space. Run for loop to print stars. Now do the same thing in reverse order. Initialize space = 1. Run for loop till n. Run for loop to print space. Increase space. Run for loop to print stars. End
Example
#include<iostream> using namespace std; main() { int n, i, j, space=1; cout<<"Enter number of rows : "; cin>>n; space=n-1; for (i=1; i<=n; i++) { for(j=1; j<=space; j++) { cout<<" "; //print space. } space--; for(j=1; j<=(2*i-1); j++) { cout<<"*"; //print stars. } cout<<"\n"; } //in reverse order. space=1; for (i=1; i<=n; i++) { for(j=1; j<=space; j++) { cout<<" "; //print space. } space++; for(j=1; j<=(2*(n-i)-1); j++) { cout<<"*"; //print stars. } cout<<"\n"; } }
Output
Enter number of rows: 4 * *** ***** ******* ***** *** *
- Related Questions & Answers
- Java Program to print the diamond shape
- Python Program to print the diamond shape
- Program to print Diamond Pattern in C
- Java Program to Print Diamond Star Pattern
- Program to print Inverse Diamond pattern on C++
- Java Program to Print Half Diamond Star Pattern
- C Program to print hollow pyramid and diamond pattern
- Program to print hollow pyramid and diamond pattern in C++
- Function to create diamond shape given a value in JavaScript?
- How to print the stars in Diamond pattern using C language?
- How to print a diamond using nested loop using C#?
- C Program for diamond pattern with different layers
- Python program to show diamond pattern with 2n-1 lines
- Program to find the perimeter of an island shape in Python
- PHP program to print the number pattern
Advertisements