How to rotate a matrix 90 degrees clockwise in C#

2 Answers

0 votes
using System;
 
class Program
{
    static void Rotate90Clockwise(int[,] matrix) {
        int rows = matrix.GetLength(0);
        
        // Step 1: Transpose the matrix
        for (int i = 0; i < rows; i++) {
            for (int j = i; j < rows; j++) {
                int temp = matrix[i, j];
                matrix[i, j] = matrix[j, i];
                matrix[j, i] = temp;
            }
        }
 
        // Step 2: Reverse each row
        for (int i = 0; i < rows; i++) {
            for (int j = 0, k = rows - 1; j < k; j++, k--) {
                int temp = matrix[i, j];
                matrix[i, j] = matrix[i, k];
                matrix[i, k] = temp;
            }
        }
    }
 
    static void PrintMatrix(int[,] matrix) {
        int rows = matrix.GetLength(0);
         
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < rows; j++) {
                Console.Write(matrix[i, j] + " ");
            }
            Console.WriteLine();
        }
    }
 
    static void Main()
    {
        int[,] matrix = {
            {1, 2, 3},
            {4, 5, 6},
            {7, 8, 9}
        };
 
        Console.WriteLine("Original Matrix:");
        PrintMatrix(matrix);
 
        Rotate90Clockwise(matrix);
 
        Console.WriteLine("\nRotated Matrix:");
        PrintMatrix(matrix);
    }
}
 
 
 
/*
run:
 
Original Matrix:
1 2 3 
4 5 6 
7 8 9 
 
Rotated Matrix:
7 4 1 
8 5 2 
9 6 3
 
*/

 



answered May 29, 2025 by avibootz
edited May 29, 2025 by avibootz
0 votes
using System;
 
class Program
{
    // Function to rotate the matrix 90 degrees clockwise
    static int[,] Rotate90Clockwise(int[,] matrix) {
        int rows = matrix.GetLength(0); // Number of rows
        int cols = matrix.GetLength(1); // Number of columns
        
        int[,] rotated = new int[cols, rows];
 
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                rotated[j, rows - 1 - i] = matrix[i, j]; // Mapping to rotated position
            }
        }

        return rotated;
    }
 
    // Function to print the matrix
    static void PrintMatrix(int[,] matrix) {
        int rows = matrix.GetLength(0);
        int cols = matrix.GetLength(1);
 
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                Console.Write(matrix[i, j] + " ");
            }
            Console.WriteLine();
        }
    }
 
    static void Main()
    {
        int[,] matrix = {
            {1, 2, 3, 4},
            {5, 6, 7, 8},
            {9, 10, 11, 12}
        };
 
        Console.WriteLine("Original Matrix:");
        PrintMatrix(matrix);
 
        int[,] rotated = Rotate90Clockwise(matrix);
 
        Console.WriteLine("\nRotated Matrix:");
        PrintMatrix(rotated);
    }
}
 
 
 
/*
run:
 
Original Matrix:
1 2 3 4 
5 6 7 8 
9 10 11 12 
 
Rotated Matrix:
9 5 1 
10 6 2 
11 7 3 
12 8 4 
 
*/

 



answered May 29, 2025 by avibootz
edited May 29, 2025 by avibootz

Related questions

2 answers 177 views
2 answers 195 views
2 answers 231 views
2 answers 176 views
2 answers 158 views
...