How to Print Matrix in Spiral Form on Console by C Language

Print Matrix in Spiral Form on Console by C Language

Introduction :

Matrix manipulation and display a matrix in a spiral form is a fundamental task in the programming world. Using programming, you can display a matrix is in a spiral form where the elements are printed in a circular, outward manner. In the Turbo C++ IDE, you can create a spiral matrix by C or C++ programming language.

In this article, I shall explore you how to print a matrix in spiral form on the console using C or C++ programming language. Here, I print matrix elements in a circular manner where it starts from the center and moving towards the outer side in an anticlockwise direction.

What is spiral matrix :

All the elements or numbers of two-dimensional array (matrix) are arranged in a spiral pattern is called spiral matrix. All the numbers of a matrix are printed either clockwise or counterclockwise which create a spiral path in the console.

There are different type of spiral matrix you can see. Among them, one is starting from the center and move outwards on the console screen. To create such spiral matrix, you have to keep track of the boundaries of the matrix and the direction (right, down, left and up) of movement.

About the program :

This is a program of spiral matrix build by C or C++ programming language. When you run the program, you have to input a number between 1 and 100. If you enter the wrong number, the program will be stopped with an error message. Otherwise, you can see a matrix in a spiral pattern on the console for your given input number.

In the console screen, you can see 1 is printed in the center. Then the number increases and move to right, up, left and finally down in a such way which form a spiral path. The program runs until all numbers are printed. To stop the program you have to press any key.

Explanation of the program :

At the beginning of the program, you have to include the stdio.h and conio.h header files. After that, define a function name as “spiralmatrix” for printing the spiral matrix. The “spiralmatrix” function takes an integer argument as input and return nothing (void). It prints the spiral matrix of numbers up to the given input on the console.

In the “spiralmatrix” function, declare integer variables such as “i”, “j”, “k”, “l” and “m” which are used to control the loop and track the position of the cursor. Using the nested while loops and a series of “if” statements to calculate the direction of movement and print the numbers in a spiral pattern. Here, you have to adjust the value of “j” and “l” based on the value of “k” variable.

The gotoxy() function is used to position the cursor at a specific location on the console screen. Now, print the current value of “k” at the appropriate position using printf() function. In the main function, first declare an integer variable “n”. Then, clear the console screen using clrscr() function. After that, ask the user for input number from 1 to 100. The input number is stored in “n” variable by scanf() function.

Now, you can check the number using “if else” statement. If user input an invalid number, an error message is displayed on the console. Otherwise, calls the spiralmatrix() function with the user input value. At last, get any key from the user using getch() function before close the program.

How run the program :

Install the Turbo C++ IDE to run the program on your pc. Then, open the Turbo C++ IDE. After that, create a C or C++ file with .c or .cpp extension in Turbo C++ IDE. Now, copy the below code and paste in your C or C++ file which you just created. Do you know how to copy paste in the Turbo C++ IDE, if you do not know follow my link. After that you can run the program on your pc to see the output.

Source code :

The following code is the source code of the spiral matrix program which builds by C or C++ programming language.

Output :

After running the spiral matrix program on your pc, you can see the result on the console like this.

output of spiral matrix by c language

Conclusion :

At the end of the article, you have learned how to create a spiral matrix using C or C++ programming language. Now, you can create more difficult programs using this concept such as image processing, game development, and more. Thanks you for visiting my site.

Scroll to Top