0% found this document useful (0 votes)
32 views7 pages

Dynamic Spiral Number Pattern Code

The document provides implementations of a spiral number pattern in multiple programming languages including C++, Python, Java, JavaScript, and C. Each implementation dynamically generates a square matrix filled with numbers in a spiral order based on user-defined size. The code snippets demonstrate how to traverse the matrix and print the resulting pattern.

Uploaded by

I KB
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views7 pages

Dynamic Spiral Number Pattern Code

The document provides implementations of a spiral number pattern in multiple programming languages including C++, Python, Java, JavaScript, and C. Each implementation dynamically generates a square matrix filled with numbers in a spiral order based on user-defined size. The code snippets demonstrate how to traverse the matrix and print the resulting pattern.

Uploaded by

I KB
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Topic :Spiral Number Pattern (Dynamic Size)

1. C++
2. Python
3. Java
4. JavaScript ([Link])
5. C
1. #include <iostream> 16. spiral[i][right] = num++; 31. for (int i = 0; i < n; i++) {

2. #include <iomanip> 17. } 32. for (int j = 0; j < n; j++) {

3. using namespace std; 18. right--; 33. cout << setw(2) << spiral[i][j] << " ";

34. }

4. void printSpiral(int n) { 19. // Traverse from right to left 35. cout << endl;

5. int spiral[n][n]; 20. for (int i = right; i >= left; i--) { 36. }

6. int num = 1; 21. spiral[bottom][i] = num++; 37. }

7. int left = 0, right = n - 1, top = 0, bottom = n - 1; 22. }

23. bottom--; 38. int main() {

8. while (left <= right && top <= bottom) { 39. int n;

9. // Traverse from left to right 24. // Traverse from bottom to top 40. cout << "Enter the size of the spiral: ";

10. for (int i = left; i <= right; i++) { 25. for (int i = bottom; i >= top; i--) { 41. cin >> n;

11. spiral[top][i] = num++; 26. spiral[i][left] = num++; 42. printSpiral(n);

12. } 27. } 43. return 0;

13. top++; 28. left++; 44. }

29. }

14. // Traverse from top to bottom

15. for (int i = top; i <= bottom; i++) { 30. // Print the spiral
1. def print_spiral(n): 12. next_x = x + directions[dir_idx][0]
2. spiral = [[0 for _ in range(n)] for _ in range(n)] 13. next_y = y + directions[dir_idx][1]
3. directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
4. dir_idx = 0 14. x, y = next_x, next_y
5. x, y = 0, 0
15. for row in spiral:
6. for num in range(1, n * n + 1): 16. print(" ".join(f"{num:2d}" for num in row))
7. spiral[x][y] = num
8. next_x = x + directions[dir_idx][0] 17. # Input
9. next_y = y + directions[dir_idx][1] 18. n = int(input("Enter the size of the spiral: "))
19. print_spiral(n)
10. if next_x < 0 or next_x >= n or next_y < 0 or
next_y >= n or spiral[next_x][next_y] != 0:
11. dir_idx = (dir_idx + 1) % 4
1. import [Link]; 15. spiral[i][right] = num++; 30. for (int i = 0; i < n; i++) {
16. } 31. for (int j = 0; j < n; j++) {
2. public class SpiralPattern { 17. right--; 32. [Link]("%2d ", spiral[i][j]);
3. public static void printSpiral(int n) { 33. }
4. int[][] spiral = new int[n][n]; 18. // Traverse from right to left 34. [Link]();
5. int num = 1; 19. for (int i = right; i >= left; i--) { 35. }
6. int left = 0, right = n - 1, top = 0, bottom = n - 20. spiral[bottom][i] = num++; 36. }
1;
21. }
22. bottom--; 37. public static void main(String[] args) {
7. while (left <= right && top <= bottom) {
38. Scanner scanner = new Scanner([Link]);
8. // Traverse from left to right
23. // Traverse from bottom to top 39. [Link]("Enter the size of the spiral:
9. for (int i = left; i <= right; i++) { ");
24. for (int i = bottom; i >= top; i--) {
10. spiral[top][i] = num++; 40. int n = [Link]();
25. spiral[i][left] = num++;
11. } 41. printSpiral(n);
26. }
12. top++; 42. }
27. left++;
43. }
28. }
13. // Traverse from top to bottom
14. for (int i = top; i <= bottom; i++) {
29. // Print the spiral
1. function printSpiral(n) { 15. right--; 29. [Link](spiral[i].map(num =>
[Link]().padStart(2, ' ')).join(' '));
2. let spiral = [Link]({ length: n }, () =>
Array(n).fill(0)); 30. }
16. // Traverse from right to left
3. let num = 1; 31. }
17. for (let i = right; i >= left; i--) {
4. let left = 0, right = n - 1, top = 0, bottom = n - 1;
18. spiral[bottom][i] = num++;
32. // Input
19. }
5. while (left <= right && top <= bottom) { 33. const readline = require('readline');
20. bottom--;
6. // Traverse from left to right 34. const rl = [Link]({
7. for (let i = left; i <= right; i++) { 35. input: [Link],
21. // Traverse from bottom to top
8. spiral[top][i] = num++; 36. output: [Link]
22. for (let i = bottom; i >= top; i--) {
9. } 37. });
23. spiral[i][left] = num++;
10. top++;
24. }
38. [Link]('Enter the size of the spiral: ', (n) => {
25. left++;
11. // Traverse from top to bottom 39. printSpiral(parseInt(n));
26. }
12. for (let i = top; i <= bottom; i++) { 40. [Link]();
13. spiral[i][right] = num++; 41. });
27. // Print the spiral
14. }
28. for (let i = 0; i < n; i++) {
1. #include <stdio.h> 15. spiral[i][right] = num++; 29. // Print the spiral
2. #include <stdlib.h> 16. } 30. for (int i = 0; i < n; i++) {
17. right--; 31. for (int j = 0; j < n; j++) {
3. void printSpiral(int n) { 32. printf("%2d ", spiral[i][j]);
4. int spiral[n][n]; 18. // Traverse from right to left 33. }
5. int num = 1; 19. for (int i = right; i >= left; i--) { 34. printf("\n");
6. int left = 0, right = n - 1, top = 0, bottom = n - 20. spiral[bottom][i] = num++; 35. }
1;
21. } 36. }
22. bottom--;
7. while (left <= right && top <= bottom) {
37. int main() {
8. // Traverse from left to right
23. // Traverse from bottom to top 38. int n;
9. for (int i = left; i <= right; i++) {
24. for (int i = bottom; i >= top; i--) { 39. printf("Enter the size of the spiral: ");
10. spiral[top][i] = num++;
25. spiral[i][left] = num++; 40. scanf("%d", &n);
11. }
26. } 41. printSpiral(n);
12. top++;
27. left++; 42. return 0;
28. } 43. }
13. // Traverse from top to bottom
14. for (int i = top; i <= bottom; i++) {

You might also like