Hi Guys, Welcome to Proto Coders Point, In this dart tutorial(article) we will learn how to print different design pattern in dart programming language.
Dart pattern program, improve your coding skillm the logic used in dart program & build logical technique used in looping concept. With my experience, pattern program are always asked in most of interview technique round.
By using loops we can print pattern in dart with different design patterns. To easily understand pattern programs in dart, we must have deep knowledge on how dart loop works such as for loop, do-while loop.
In this tutorial we will learn how to print star pattern, number pattern, character pattern in dart programming language.
Learn basic of how to print pattern in dart loop works
Before we go to dart program to print pattern design, let’s learn basic of pattern printing.
Suppose you want ot draw a pattern using dart program, directly don’t start writng program.
First draw the pattern in a table block using row & column, so that you can understand the logic flow, which block to fill and which block to keep empty.
As we have shown in below star pattern image.
To build/draw a pattern in console a program must use atleast two loop, if the program is complex to print pattern we must use more looping techniques.
then, if you observed above pattern image, the first row print only 1 star, the second row print 2 stars & the third row prints 3 stars and so on.
Understanding the logic behind printing pattern in dart
Let’s understand the logic behind how the loop will iterate through loop to print stars patterns as shown in above pattern image.
Snippet code
for(int i = 0 ; i< 5; i++) { for(int j = 0; j<=i;j++) { stdout.write('*'); } stdout.writeln(); }
Here the first for loop is for row (i) & second for loop is column (j).
let’s go, execution of code step by step in iteration wise.
iteration 1:
for i = 0 ; i < 4 (true) for k = 0; j <= i (true)
In first iteration only 1 star get printed because J loop runs for 1 time & then comes out of loop & jumps out of j loop & goes to next line.
*
now, the value of i is incremented by +1 & j is rest to 0.
iteration 2:
for i = 1 ; i < 4 (true) for k = 0; j <= i (true)
In second iteration, j loop runs for 2 times, so two stars get prined & jump out of loop & goes to next line.
* * *
now, the value of i is incremented by +1 & j is rest to 0.
iteration 3:
for i = 2 ; i < 4 (true) for k = 0; j <= i (true)
In third iteration, j loop runs for 3 times, i.e. j = 0 to j = 2 (0 – 1 -2). so stars get printed 3 times.
* * * * * *
now, the value of i is incremented by +1 & j is rest to 0.
iteration 4:
for i = 3 ; i < 4 (true) for k = 0; j <= i (true)
In fourth iteration, j loop runs for 4 times, i.e. j = 0 to j = 3 (0 – 1 -2 – 3). so stars get printed 4 times.
* * * * * * * * * *
now, the value of i is incremented by +1 & j is rest to 0.
iteration 5 will not work
for i = 4 ; i < 4 (false)
Here, the execution of loop stops, as i < 4. i.e. 4 < 4 which is false. therefore the loop stops.
Star pattern in dart
1. Right triangle star pattern
import 'dart:io'; void main(){ int rows = 6; for(int i = 0 ; i< rows; i++) { for(int j = 0; j<=i;j++) { stdout.write('* '); } stdout.writeln(); } }
2. Left triangle star pattern
import 'dart:io'; void main(){ int rows = 6; for(int i = 0 ; i< rows; i++) { for(int j=2*(rows-i);j>=0;j--){ stdout.write(" "); } for(int j = 0;j<=i;j++) { stdout.write("* "); } stdout.writeln(); } }
3. Pyramid star pattern
import 'dart:io'; void main(){ int rows = 6; for(int i = 0 ; i< rows; i++) { for(int j=(rows-i);j>1;j--){ stdout.write(" "); } for(int j = 0;j<=i;j++) { stdout.write("* "); } stdout.writeln(); } }
4. Reverse Pyramid star pattern
import 'dart:io'; void main(){ int rows = 6; for(int i = 0 ; i< rows; i++) { for(int j = 0;j<=i;j++) { stdout.write(" "); } for(int k=0;k<=rows-1-i;k++){ stdout.write("* "); } stdout.writeln(); } }
5. Sandglass star pattern
import 'dart:io'; void main() { int i, j, k, n = 10; for (i = 0; i <= n - 1; i++) { for (j = 0; j < i; j++) { stdout.write(" "); } for (k = i; k <= n - 1; k++) { stdout.write("*" + " "); } stdout.writeln(""); } for (i = n - 1; i >= 0; i--) { for (j = 0; j < i; j++) { stdout.write(" "); } for (k = i; k <= n - 1; k++) { stdout.write("*" + " "); } stdout.writeln(""); } }
6. Christmas Tree Star Pattern
import 'dart:io'; void main() { //beginning of upper part for (int i = 1; i <= 6; i++) { for (int j =6 - i; j > 0; j--) { stdout.write(" "); } for (int k = 1; k <= i; k++) { stdout.write("* "); } stdout.writeln(); } //end of upper part //beginning of lower part for (int i = 1; i <= 6 - 1; i++) { stdout.write(" "); for (int j = 6 - 3; j > 0; j--) { stdout.write(" "); } for (int k = 2; k > 0; k--) { stdout.write("| "); } stdout.writeln(); } // end of lower part }
Number Pattern in dart
1. Right triangle Number patterns
import 'dart:io'; void main() { int i, j, numbers, n = 10; for(i=0;i<n;i++){ numbers = 1; for(j = 0; j<=i;j++) { stdout.write('$numbers '); numbers++; } stdout.writeln(); } }
2. Number Increment pattern
import 'dart:io'; void main() { int i, j, numbers=1, n = 10; for(i=1;i<=n;i++){ for(j = 1; j<i+1;j++) { stdout.write('${numbers++} '); } stdout.writeln(); } }
3. Sandglass Number patterns
import 'dart:io'; void main() { int n = 9, i, j; for (int i = 1; i <= n; i++) { for (int j = 1; j < i; j++) { stdout.write(" "); } for (int k = i; k <= n; k++) { stdout.write("$k "); } stdout.writeln(); } for (int i = n - 1; i >= 1; i--) { for (int j = 1; j < i; j++) { stdout.write(" "); } for (int k = i; k <= n; k++) { stdout.write("$k "); } stdout.writeln(); } }
4. Rotated pyramid number pattern
import 'dart:io'; void main() { int rows = 8; //Prints upper half pattern for (int i = 1; i <= rows; i++) { for (int j = 1; j <= i; j++) { stdout.write("$j "); } stdout.writeln(); } //prints lower half pattern for (int i = rows - 1; i >= 1; i--) { for (int j = 1; j <= i; j++) { stdout.write("$j "); } stdout.writeln(); } }
Character Pattern in dart
Converting string in ascii code in dart
1. Right Triangle Alphabetic Pattern in dart
import 'dart:io'; void main() { int alphabet = 65; for (int i = 0; i <= 8; i++) { for(int j = 0;j<=i;j++) { stdout.write('${String.fromCharCode(alphabet+j)} '); } stdout.writeln(); } }
2. Repeating Alphabet Pattern
import 'dart:io'; void main() { int alphabet = 65; for (int i = 0; i <= 8; i++) { for(int j = 0;j<=i;j++) { stdout.write('${String.fromCharCode(alphabet)} '); } alphabet++; stdout.writeln(); } }
3. K-shape Alphabet Pattern
import 'dart:io'; void main() { for (int i = 8; i >= 0; i--) { int alphabet = 65; for(int j = 0;j<=i;j++) { stdout.write('${String.fromCharCode(alphabet+j)} '); } stdout.writeln(); } for (int i = 0; i <= 8; i++) { int alphabet = 65; for(int j = 0;j<=i;j++) { stdout.write('${String.fromCharCode(alphabet+j)} '); } stdout.writeln(); } }