GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing codescracker.com — C++ program to print a diamond pattern
C++ program to print a diamond patternIn this article, you will learn and get code to print a diamond pattern in C++. Here is the list of programs on the printing of diamond patterns available in this article:
In C++, print a diamond pattern of starsTo print a diamond pattern of stars in C++ programming, you have to ask the user to enter the number of rows. Now, using the row size, print the diamond pattern as shown in the program given below: If the user enters 6 as the row size of a diamond, then its upper-triangular part expands up to 6 lines, whereas its lower-triangular part expands up to 5 (one less than the row size) lines. The question is, "Write a program in C++ to print a diamond pattern of stars." Here is its answer: #include<iostream> using namespace std; int main() { int i, j, rowNum, space; cout<<"Enter the Number of Rows: "; cin>>rowNum; space = rowNum-1; for(i=1; i<=rowNum; i++) { for(j=1; j<=space; j++) cout<<" "; space--; for(j=1; j<=(2*i-1); j++) cout<<"*"; cout<<endl; } space = 1; for(i=1; i<=(rowNum-1); i++) { for(j=1; j<=space; j++) cout<<" "; space++; for(j=1; j<=(2*(rowNum-i)-1); j++) cout<<"*"; cout<<endl; } cout<<endl; return 0; } This program was built and runs under the Code::Blocks IDE. Here is its sample run: Now supply the input, say 6 as the number of rows, and press the ENTER key to print the diamond pattern as shown in the snapshot given below: Note that the row size in this case is the upper triangular part of the diamond pattern, not the actual row size. There are three for loops used to create both the upper and lower triangles of a diamond. The first three for loops are created in a way that:
And the three for loops for lower triangular part of the diamond pattern also works in a similar way. The dry run of the following block of code: space = rowNum-1; for(i=1; i<=rowNum; i++) { for(j=1; j<=space; j++) cout<<" "; space--; for(j=1; j<=(2*i-1); j++) cout<<"*"; cout<<endl; } that prints the upper triangular part of a diamond, goes like:
In this way, the star pattern of the upper diamond gets printed, and in a similar way, the lower diamond also gets printed. Print a diamond pattern of numbersThis is the same program as the previous one. The only difference is that instead of using stars, we've used numbers to print a diamond pattern of numbers. #include<iostream> using namespace std; int main() { int i, j, rowNum, space, num=1; cout<<"Enter the Number of Rows: "; cin>>rowNum; space = rowNum-1; for(i=1; i<=rowNum; i++) { for(j=1; j<=space; j++) cout<<" "; space--; for(j=1; j<=(2*i-1); j++) { cout<<num; num++; } cout<<endl; num = 1; } space = 1; for(i=1; i<=(rowNum-1); i++) { for(j=1; j<=space; j++) cout<<" "; space++; for(j=1; j<=(2*(rowNum-i)-1); j++) { cout<<num; num++; } cout<<endl; num = 1; } cout<<endl; return 0; } Here is its sample run with user input, 5: Print a diamond pattern of alphabet charactersThis is the last program that creates a diamond pattern of alphabet characters. #include<iostream> using namespace std; int main() { int i, j, rowNum, space; char ch='A'; cout<<"Enter the Number of Rows: "; cin>>rowNum; space = rowNum-1; for(i=1; i<=rowNum; i++) { for(j=1; j<=space; j++) cout<<" "; space--; for(j=1; j<=(2*i-1); j++) { cout<<ch; ch++; } cout<<endl; ch = 'A'; } space = 1; for(i=1; i<=(rowNum-1); i++) { for(j=1; j<=space; j++) cout<<" "; space++; for(j=1; j<=(2*(rowNum-i)-1); j++) { cout<<ch; ch++; } cout<<endl; ch = 'A'; } cout<<endl; return 0; } Here is a sample run with user input and a row size of 5: The same program in different languages« Previous Program Next Program » Practice Quizzes
Test your programming and computer science knowledge with interactive MCQs and topic-based quizzes. Popular Topics
Explore the most popular programming and computer science tutorials on CodesCracker. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |