In this tutorial, we will discuss the concept of Cpp program to Display diamond number pattern using while loop
In this post, we will learn how to create diamond number pattern using while loop in C++ language
We can use for loop, while loop or do while loop to display different diamond Number patterns.
Here, we will use while loop to print diamond Number patterns
The following C++ program requests the user to enter the number of rows as the user wanted and the program will display the number of rows in diamond number pattern according to the user input
Program 1
#include <iostream> #include <conio.h> using namespace std; int main() { int rows; cout<<"Enter the number of rows: "; cin>>rows; int row=1,i,j,k; i=1; while(i<=rows){ //print upper part for(j=1; j<=rows-i; j++){ cout<<" "; } k=1; while(k<=i){ cout<<k;//print number for upper part cout<<" "; k++; } cout<<"\n"; i++; } i=1; while(i<=rows-1){ //print lower part j=1; while(j<=i){ cout<<" "; j++; } k=1; while(k<=rows-i){ cout<<k;//print number for lower part cout<<" "; k++; } cout<<"\n"; i++; } getch(); return 0; }
When the above code is executed, it produces the following result
Enter the number of rows: 6 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 5 6 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1
Program 2
#include <iostream> using namespace std; int main() { int i,j,k,l,rows; cout<<"Enter the number of rows"; cin>>rows; i=1; while(i<=rows){ //print upper part j=1; while(j<=rows-i){ cout<<" ";//print initial space for upper part j++; } k=i; while(k>=1){ cout<<k; k--; } l=2; while(l<=i){ cout<<l; l++; } cout<<"\n"; i++; } i=rows-1; while(i>=1){ //print lower part j=0; while(j<=rows-1-i){ cout<<" ";//print initial space for lower part j++; } k=i; while(k>=1){ cout<<k; k--; } l=2; while(l<=i){ cout<<l; l++; } cout<<"\n"; i--; } return 0; }
When the above code is executed, it produces the following result
Enter the number of rows: 6 1 212 32123 4321234 543212345 65432123456 543212345 4321234 32123 212 1
Program 3
#include <iostream> #include <conio.h> using namespace std; int main() { int rows,i,j; int row=1; cout<<"Enter the number of rows: "; cin>>rows; i=rows/2; while(i>0){ //print upper part j=1; while(j<=i){ cout<<" "; j++; } j=1; while(j<=row){ cout<<row; cout<<" "; j++; } cout<<"\n"; row++; i--; } i=0; while(i<=rows/2){ j=1; while(j<=i){ cout<<" "; j++; } j=row; while(j>=1){ //print lower part cout<<row; cout<<" "; j--; } cout<<"\n"; row--; i++; } getch(); return 0; }
When the above code is executed, it produces the following result
Enter the number of rows: 8 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 4 4 4 4 3 3 3 2 2 1
Similar post
Java program to Display diamond number pattern using while loop
C++ code to Display diamond number pattern using while loop
Floyd’s triangle number pattern using for loop in C
Floyd’s triangle pattern using nested for loop in Java
Floyd’s triangle pattern using nested while loop in Java
Hollow pyramid triangle pattern in C++ language
Rhombus pattern in Java using for loop
Rhombus pattern in C using while loop
Rhombus pattern in C++ using do-while loop
Display Pyramid star pattern in C
Display Pyramid star pattern in Java
Parallelogram and Hollow Parallelogram star pattern in Java
Parallelogram and Hollow Parallelogram star pattern in C Language
C++ program to Display diamond number pattern using while loop
C program to Display diamond number pattern using while loop
Suggested for you
Nested for loop in Java language
Nested for loop in C++ language
Nested for loop in Python language
If statements in Java language
Nested if statements in Java language
How to find reverse number using method In this article, we will discuss the concept…
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…
View Comments
That's a great website for students.