Write a C++ program to print the diamond number pattern using for loop.
#include<iostream> using namespace std; int main() { int i, j, k, rows; cout << "Enter Diamond Number Pattern Row = "; cin >> rows; cout << "Diamond Number Pattern\n"; for(i = 1; i <= rows; i++) { for(j = 1; j <= rows - i; j++) { cout << " "; } for(k = 1; k <= i * 2 - 1; k++) { cout << k; } cout << "\n"; } for(i = rows - 1; i > 0; i--) { for(j = 1; j <= rows - i; j++) { cout << " "; } for(k = 1; k <= i * 2 - 1; k++) { cout << k; } cout << "\n"; } return 0; }
This C++ example is another way of printing the diamond pattern of numbers.
#include<iostream> using namespace std; int main() { int i, j, k, l, rows; cout << "Enter Diamond Number Pattern Row = "; cin >> rows; cout << "Diamond Number Pattern\n"; for(i = 1; i <= rows; i++) { for(j = 1; j <= rows - i; j++) { cout << " "; } for(k = i; k >= 1; k--) { cout << k; } for(l = 2; l <= i; l++) { cout << l; } cout << "\n"; } for(i = rows - 1; i > 0; i--) { for(j = 1; j <= rows - i; j++) { cout << " "; } for(k = i; k >= 1; k--) { cout << k; } for(l = 2; l <= i; l++) { cout << l; } cout << "\n"; } return 0; }
Enter Diamond Number Pattern Row = 9
Diamond Number Pattern
1
212
32123
4321234
543212345
65432123456
7654321234567
876543212345678
98765432123456789
876543212345678
7654321234567
65432123456
543212345
4321234
32123
212
1