Skip to main content

Featured

C Program to Solve Two Sum Using Brute Force (With Algorithm & Output)

 Introduction The Two Sum problem is a popular coding interview question where we must find two indices of an array whose values add up to a given target. This program demonstrates a simple brute-force solution in C using nested loops and dynamic memory allocation. Problem Statement Given an integer array and a target value, return the indices of the two numbers such that they add up to the target. Each input has exactly one solution, and the same element cannot be used twice. The result should return the indices, not the values. If no solution exists, return NULL.  Algorithm / Logic Explanation Start the program. Traverse the array using a loop from index 0 to numsSize - 1 . Inside this loop, use another loop starting from i + 1 to numsSize - 1 . For every pair (i, j) , check if nums[i] + nums[j] == target . If condition becomes true: Allocate memory for 2 integers using malloc() . Store indices i and j . Set returnSize = 2 . Return the result poi...

Transpose of Matrix in C

Transpose of a Matrix in C

✅ Transpose of a Matrix in C

#include <stdio.h>

int main() {
    int row, col;

    printf("Enter number of rows and columns:\n");
    scanf("%d %d", &row, &col);

    int matrix[row][col], transpose[col][row];

    printf("Enter %d elements in the matrix:\n", row * col);
    for(int i = 0; i < row; i++) {
        for(int j = 0; j < col; j++) {
            scanf("%d", &matrix[i][j]);
        }
    }

    // Print Original Matrix
    printf("\nOriginal Matrix:\n");
    for(int i = 0; i < row; i++) {
        for(int j = 0; j < col; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }

    // Transpose the matrix
    for(int i = 0; i < row; i++) {
        for(int j = 0; j < col; j++) {
            transpose[j][i] = matrix[i][j];
        }
    }

    // Print Transposed Matrix
    printf("\nTransposed Matrix:\n");
    for(int i = 0; i < col; i++) {
        for(int j = 0; j < row; j++) {
            printf("%d ", transpose[i][j]);
        }
        printf("\n");
    }

    return 0;
}
  

๐Ÿ“˜ Explanation:

In this program, we read a 2D matrix from the user and then print its transpose. A transpose of a matrix is formed by interchanging its rows and columns. For example, if element matrix[i][j] is at row i and column j, then it is placed at transpose[j][i] in the transposed matrix.

This method is useful in applications like image processing, solving systems of linear equations, and matrix algebra.

The user first inputs the number of rows and columns. The program then takes all matrix values, prints the original matrix, computes its transpose, and prints the result.

๐Ÿงพ Sample Output:

Enter number of rows and columns:
2 3
Enter 6 elements in the matrix:
1 2 3
4 5 6

Original Matrix:
1 2 3
4 5 6

Transposed Matrix:
1 4
2 5
3 6
  

๐Ÿ”‘ Keywords:

Transpose of Matrix in C, Matrix Transpose C Program, C language 2D Arrays, Matrix Programs in C, C Program with Input and Output, Matrix Logic in C, Transpose Algorithm in C, C Coding Examples, Beginner C Programs

๐Ÿ“Œ Hashtags:

#CProgramming #MatrixTranspose #CPrograms #2DArrays #TransposeInC #CodingInC #BeginnerFriendly #CLanguage

Comments

Popular Posts

๐ŸŒ™