0% found this document useful (0 votes)
8 views

Yudiz Solutions (patterns)

The document provides a collection of easy and medium-level pattern programs in JavaScript, including various shapes made with asterisks and numbers. It features examples such as squares, triangles, pyramids, and combinations of numbers and stars, along with their respective code snippets and output. The document encourages experimentation with these patterns and offers to provide more complex variations if needed.

Uploaded by

HEMAL PATEL
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Yudiz Solutions (patterns)

The document provides a collection of easy and medium-level pattern programs in JavaScript, including various shapes made with asterisks and numbers. It features examples such as squares, triangles, pyramids, and combinations of numbers and stars, along with their respective code snippets and output. The document encourages experimentation with these patterns and offers to provide more complex variations if needed.

Uploaded by

HEMAL PATEL
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Yudiz Solutions(patterns program)

Here are some **easy** and **medium-level pattern programs** in JavaScript:

---

### **Easy Pattern Programs:**

1. **Print a Square of `*`**


```js
function printSquare(n) {
for (let i = 0; i < n; i++) {
console.log('* '.repeat(n));
}
}
printSquare(5);
```

Output for `n=5`:


```
*****
*****
*****
*****
*****
```

2. **Right-Angled Triangle of `*`**


```js
function rightAngledTriangle(n) {
for (let i = 1; i <= n; i++) {
console.log('* '.repeat(i));
}
}
rightAngledTriangle(5);
```

Output for `n=5`:


```
*
**
***
****
*****
```

3. **Inverted Right-Angled Triangle of `*`**


```js
function invertedRightTriangle(n) {
for (let i = n; i >= 1; i--) {
console.log('* '.repeat(i));
}
}
invertedRightTriangle(5);
```

Output for `n=5`:


```
*****
****
***
**
*
```

4. **Half Pyramid with Numbers**


```js
function numberHalfPyramid(n) {
for (let i = 1; i <= n; i++) {
let row = '';
for (let j = 1; j <= i; j++) {
row += j + ' ';
}
console.log(row);
}
}
numberHalfPyramid(5);
```

Output for `n=5`:


```
1
12
123
1234
12345
```

5. **Floyd's Triangle**
```js
function floydTriangle(n) {
let num = 1;
for (let i = 1; i <= n; i++) {
let row = '';
for (let j = 1; j <= i; j++) {
row += num + ' ';
num++;
}
console.log(row);
}
}
floydTriangle(5);
```

Output for `n=5`:


```
1
23
456
7 8 9 10
11 12 13 14 15
```

---
### **Medium Pattern Programs:**

1. **Pyramid Pattern**
```js
function pyramid(n) {
for (let i = 1; i <= n; i++) {
let space = ' '.repeat(n - i);
let stars = '* '.repeat(i);
console.log(space + stars);
}
}
pyramid(5);
```

Output for `n=5`:


```
*
**
***
****
*****
```

2. **Inverted Pyramid Pattern**


```js
function invertedPyramid(n) {
for (let i = n; i >= 1; i--) {
let space = ' '.repeat(n - i);
let stars = '* '.repeat(i);
console.log(space + stars);
}
}
invertedPyramid(5);
```

Output for `n=5`:


```
*****
****
***
**
*
```

3. **Diamond Pattern**
```js
function diamond(n) {
for (let i = 1; i <= n; i++) {
let space = ' '.repeat(n - i);
let stars = '* '.repeat(i);
console.log(space + stars);
}
for (let i = n - 1; i >= 1; i--) {
let space = ' '.repeat(n - i);
let stars = '* '.repeat(i);
console.log(space + stars);
}
}
diamond(5);
```

Output for `n=5`:


```
*
**
***
****
*****
****
***
**
*
```

4. **Hollow Square Pattern**


```js
function hollowSquare(n) {
for (let i = 1; i <= n; i++) {
if (i === 1 || i === n) {
console.log('* '.repeat(n));
} else {
console.log('* ' + ' '.repeat(n - 2) + '*');
}
}
}
hollowSquare(5);
```
Output for `n=5`:
```
*****
* *
* *
* *
*****
```

5. **Pascal’s Triangle**
```js
function pascalTriangle(n) {
let arr = [];
for (let i = 0; i < n; i++) {
arr[i] = new Array(i + 1);
for (let j = 0; j <= i; j++) {
if (j === 0 || j === i) {
arr[i][j] = 1;
} else {
arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j];
}
process.stdout.write(arr[i][j] + " ");
}
console.log();
}
}
pascalTriangle(5);
```
Output for `n=5`:
```
1
11
121
1331
14641
```

---

These programs cover both basic and medium-difficulty levels. You can further modify
them to experiment with different patterns. Let me know if you need more complex
patterns or further explanations!

Here are some pattern programs that combine **numbers** and **stars (`*`)**:

---

### **Easy Combination of Numbers and Stars:**

1. **Triangle of Numbers and Stars**


```js
function numberStarTriangle(n) {
for (let i = 1; i <= n; i++) {
let row = '';
for (let j = 1; j <= i; j++) {
row += j + ' ';
}
for (let k = i; k < n; k++) {
row += '* ';
}
console.log(row);
}
}
numberStarTriangle(5);
```

Output for `n=5`:


```
1****
12***
123**
1234*
12345
```

2. **Inverted Number and Star Pattern**


```js
function invertedNumberStarPattern(n) {
for (let i = n; i >= 1; i--) {
let row = '';
for (let j = 1; j <= i; j++) {
row += j + ' ';
}
for (let k = i; k < n; k++) {
row += '* ';
}
console.log(row);
}
}
invertedNumberStarPattern(5);
```

Output for `n=5`:


```
12345
1234*
123**
12***
1****
```

---

### **Medium-Level Combination of Numbers and Stars:**

1. **Pyramid Pattern with Numbers and Stars**


```js
function pyramidNumberStar(n) {
for (let i = 1; i <= n; i++) {
let row = '';
for (let j = 1; j <= n - i; j++) {
row += ' '; // Adding space for alignment
}
for (let j = 1; j <= i; j++) {
row += j + ' ';
}
for (let k = 1; k < i; k++) {
row += '* ';
}
console.log(row);
}
}
pyramidNumberStar(5);
```

Output for `n=5`:


```
1
12*
123**
1234***
12345****
```

2. **Hourglass Pattern of Numbers and Stars**


```js
function hourglassNumberStar(n) {
for (let i = n; i >= 1; i--) {
let row = '';
for (let j = n; j > i; j--) {
row += '* ';
}
for (let k = 1; k <= i; k++) {
row += k + ' ';
}
console.log(row);
}
for (let i = 2; i <= n; i++) {
let row = '';
for (let j = n; j > i; j--) {
row += '* ';
}
for (let k = 1; k <= i; k++) {
row += k + ' ';
}
console.log(row);
}
}
hourglassNumberStar(5);
```

Output for `n=5`:


```
12345
*1234
**123
***12
****1
***12
**123
*1234
12345
```

3. **Diamond Pattern with Numbers and Stars**


```js
function diamondNumberStar(n) {
for (let i = 1; i <= n; i++) {
let row = ' '.repeat(n - i);
for (let j = 1; j <= i; j++) {
row += j + ' ';
}
for (let k = 1; k < i; k++) {
row += '* ';
}
console.log(row);
}
for (let i = n - 1; i >= 1; i--) {
let row = ' '.repeat(n - i);
for (let j = 1; j <= i; j++) {
row += j + ' ';
}
for (let k = 1; k < i; k++) {
row += '* ';
}
console.log(row);
}
}
diamondNumberStar(5);
```
Output for `n=5`:
```
1
12*
123**
1234***
12345****
1234***
123**
12*
1
```

---

These programs showcase the combination of numbers and stars in various formats like
triangles, pyramids, and diamonds. Let me know if you need further variations or
explanations!

You might also like