Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
799 views

Patterns Dart

The document provides examples of Dart code to generate different patterns. It includes 6 code snippets to print patterns such as increasing numbers, letters with spaces, Pascal's triangle, and stars. Each code snippet demonstrates how to use loops and conditionals to control printing values and spaces to generate the pattern. The output of running each code snippet is not shown.

Uploaded by

Vandana Garia
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
799 views

Patterns Dart

The document provides examples of Dart code to generate different patterns. It includes 6 code snippets to print patterns such as increasing numbers, letters with spaces, Pascal's triangle, and stars. Each code snippet demonstrates how to use loops and conditionals to control printing values and spaces to generate the pattern. The output of running each code snippet is not shown.

Uploaded by

Vandana Garia
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Set 2

Using Dart Programming Language create the following patterns

1.

void main() {
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= i; j++) {
print("*");
}
print("");
}
}

Output 

2.

void main() {

int n = 4; // Number of rows for the pattern

int currentNumber = 1;

for (int i = 1; i <= n; i++) {

for (int j = 1; j <= i; j++) {

// Print the current number and a space

print('$currentNumber ');

currentNumber++;

}
// Move to the next line after each row

print('');

Output 

3.

void main() {

int n = 4; // Number of rows for the pattern

for (int i = 1; i <= n; i++) {

// Print spaces before the numbers

for (int j = 1; j <= n - i; j++) {

print(' '); // 4 spaces for each number

// Print increasing numbers

for (int j = 1; j <= i; j++) {

print('${j.toString().padLeft(4)}');

// Print decreasing numbers


for (int j = i - 1; j >= 1; j--) {

print('${j.toString().padLeft(4)}');

print(''); // Move to the next line

4.

void main() {

List<String> words = ["F", "L U", "T T E", "R &", "D A R T"];

for (int i = 0; i < words.length; i++) {

String word = words[i];

int spaces = (words.length - 1 - i) * 2;

for (int j = 0; j < spaces; j++) {

print(' '); // Print spaces

for (int j = 0; j < word.length; j++) {

if (word[j] != ' ') {

print(word[j]); // Print letters

} else {

print(' '); // Print extra spaces

}
}

print(''); // Move to the next line

Output 

5.

void main() {

int numRows = 6; // Number of rows for Pascal's Triangle

for (int i = 0; i < numRows; i++) {

// Print spaces before each row

for (int j = 0; j < numRows - i; j++) {

print(' '); // Three spaces for each number

int number = 1; // The first number in each row is always 1

for (int j = 0; j <= i; j++) {

// Print the current number and spaces

print('${number.toString().padLeft(3)} ');
// Calculate the next number based on the previous number

number = number * (i - j) ~/ (j + 1);

print(''); // Move to the next line

Output 

6.

void main() {

int numRows = 5; // Number of rows for the star pattern

// Print the top half of the star pattern

for (int i = 1; i <= numRows; i++) {

// Print spaces before stars

for (int j = 1; j <= numRows - i; j++) {

print(' '); // Three spaces for each star

// Print stars

for (int j = 1; j <= i * 2 - 1; j++) {

print(' * ');
}

print(''); // Move to the next line

// Print the bottom half of the star pattern

for (int i = numRows - 1; i >= 1; i--) {

// Print spaces before stars

for (int j = 1; j <= numRows - i; j++) {

print(' '); // Three spaces for each star

// Print stars

for (int j = 1; j <= i * 2 - 1; j++) {

print(' * ');

print(''); // Move to the next line

Output 

You might also like