w3codeworld logo
  • Home
  • C
  • C++
  • Java
  • Python3
  • JavaScript
  • C#
  • PHP
  • Codeigniter
  • Laravel
  • Articles
  1. Home
  2. Articles
  3. Write a c program to rotate the matrix by k times in a clockwise direction using the function
  • PHP
  • Codeigniter
  • Laravel
  • C
  • C++
  • Java
  • Python3
  • JavaScript
  • C#

Write a c program to rotate the matrix by k times in a clockwise direction using the function

  • Author: Admin
  • Last Updated: 2021-01-01
  • Category: C

Write a c program to rotate the matrix by k times in a clockwise direction using the function. In this program, you will learn how to rotate the matrix by k times in a clockwise direction using the function.

 

Take an example to make this clockwise rotation through a c program:

// Write a c program to rotate the matrix by k times
// in a clockwise direction using the function
#include <stdio.h>

// It's the matrix's dimensions
#define M 3
#define N 3

// It's the driver function
// to make clockwise rotation 
void rotateClockWise(int arru[][M], int k)
{ 
    int temp[M];
    k = k % M;

    for (int i = 0; i < N; i++)
    {
        for (int t = 0; t < M - k; t++)
            temp[t] = arru[i][t];

        for (int j = M - k; j < M; j++)
            arru[i][j - M + k] = arru[i][j];

        for (int j = k; j < M; j++)
            arru[i][j] = temp[j - k];
    }
}

// This will display the matrix 
void displayMatrixData(int arru[][M])
{
    for (int i = 0; i < N; i++)
    {
        printf("\t");
        for (int j = 0; j < M; j++)
            printf("%d\t", arru[i][j]);
        printf("\n");
    }
}

int main()
{
    int arru[N][M] = {
        { 12, 13, 14 },
        { 25, 26, 27 },
        { 38, 39, 31 }
    };
    int k = 2;

    printf("-----This is the given matrix before rotation-----\n\n");
    displayMatrixData(arru);

    // It will rotate matrix by k 
    rotateClockWise(arru, k);

    printf("\n\n------This is the given matrix after rotation %d times------\n\n", k);

    // It will display rotated matrix 
    displayMatrixData(arru);

    return 0;
}

 

Output:

-----This is the given matrix before rotation-----

    12    13    14    
    25    26    27    
    38    39    31    


------This is the given matrix after rotation 2 times------

    13    14    12    
    26    27    25    
    39    31    38

 

Tags:

# write a c program to rotate matrix clockwise

# write a c program to rotate matrix anticlockwise

# write a c program to rotate each ring of matrix anticlockwise by k elements

 

Related Articles

Write a c program to make a circular rotation of an array by k positions using the loops Write a c program to rotate the matrix by k times in a clockwise direction using the function Write a c program to print a hollow square star pattern with diagonal using loops (for and while loop) How to rearrange positive and negative numbers in array in c language How to print a star pattern without using loop in the c programming language Write a program to find the decimal to octal conversion in c language using the while loop Write a c program to convert an array into zig-zag fashion using the function Write a program of spiral pattern printing in c language Write a c program to find the sum of a zig-zag pattern in a given matrix using the recursion Write a program to find the transpose of a matrix in c language How to print the hollow diamond pattern in c language How to find the sum of each row and column of a matrix in c language Write a program to make transformation matrix rotation 90 degrees clockwise in c language Write a program to check an integer number is an automorphic number or not in the c language Write a c program by using call by reference in cyclic order to swap the two numbers Write a c program to convert binary to decimal and vice versa using the function Write a c program to store information of a student using the structure with the for loop Make a c program to reverse a sentence using recursion How to write a c program to find the sum of natural numbers using recursion Write a c program to display characters from a to z using the for loop Write a program to find quotient and remainder in c language Write a program to print the matrix pattern in c language using the for loop Write a program to print the rhombus pattern in the c language of the stars using the for loop Write a program to draw the half pyramid pattern in c language of stars using the for loop Write a c program to draw a rectangle using for loop Write a program to print the left & right arrow pattern in c language Write a program to print Floyd's triangle number pattern in c language Write a program to print the pascal triangle pattern in c language Write a program to print the butterfly pattern of the numbers in c programming by using the for loop C program to print diamond pattern of stars by using the for loop C program to check whether a character is an alphabet or not by using the if-else statement and using the conditional operator Write a program to check an integer number is a prime or composite number in c language by using the while & for loop How to check whether a number is an integer or float in c language by using the while loop C program to find factors of a number using while loop & for loop C program to check the input integer number is an Armstrong number using the for loops & while loops How to swap the two integers or real numbers without using the third variable in the c language C program to convert the reverse case of an input character C program to check leap year using the if-else and by using the switch case statement C program to find the roots of a quadratic equation using the sqrt() function with the if-else condition C program to find GCD of two numbers using the while loop and using the for loop C program to find LCM of two numbers using for loop and with GCD calculation C program to calculate the power of N number using for loop and by using pow function C program to find the Normal & Trace of a square matrix by using the for loop C program to multiply two same dimension matrices by using the for loop C program to find the SUM of the N input numbers using arrays with for loop C program to find the largest and smallest element in an array by using for loop C program to print the multiplication table of any integer number with multiplication range C program to print prime numbers from 1 to N using for loop C program to check input number is even or odd using if-else statement and with conditional operator C program to find the average of N numbers by using for loop
Recent Articles
  • Write a program to find all the patterns of 0(1+)0 in the given string
  • C# program to enter two numbers and find their sum
  • Write a c++ program to store information of a student using the structure with the for loop
  • Write a c program to store information of a student using the structure with the for loop
  • How to write a c++ program to find the sum of natural numbers using recursion
  • How to write a c program to find the sum of natural numbers using recursion
  • Write a python program to convert an array into zig-zag fashion using the function
  • C program to perform input/output of all basic data types
  • Write a python program to print a hollow square star pattern with diagonal using loops (for and while loop)
  • Write a c program to make a circular rotation of an array by k positions using the loops
View All Articles
  W3Codeworld

Get the latest updates

  • ABOUT US
  • About Company
  • Contact Us
  • Terms Conditions
  • Privacy Policy
  • Top Tutorials
  • PHP
  • Codeigniter
  • Laravel
  • Top Articles
  • C
  • C++
  • Java
  • Python
  • JavaScript
  • PHP

© Copyright 2020-2021. All Rights Reserved.