Saturday, March 28, 2026

Write a program for display values reverse order from an array using a pointer || C Lab Program

 WAP_C05: Write a program for display values reverse order from an array using a pointer.|| Arrays, Pointers and Functions

WAP_C05: C Lab Program


//display values reverse order from an array using a pointer.
#include <stdio.h>

int main() {
    int n, sum = 0, arr[10];

    // Input the number of elements
    printf("Enter the number of elements: ");
    scanf("%d", &n);

    // Input the array elements
    printf("Enter the elements:\n");
    for (int i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }
   
    int *ptr = arr; 
   
    // Display the array elements in reverse order
    printf("The elements in reverse order are:\n");
    for (int i = n-1; i >= 0; i--) {
      printf("%2d", *(ptr+i));
    }

    return 0;
}



OUTPUT


Enter the number of elements: 5
Enter the elements:
2 4 6 8 10
The elements in reverse order are:
10 8 6 4 2


For all 2026 published articles list: click here

...till the next post, bye-bye & take care.

Friday, March 27, 2026

Write a program for reading elements using a pointer into an array and display the values using the array || C Lab Program

 WAP_C04 Write a program for reading elements using a pointer into an array and display the values using the array.|| Arrays, Pointers and Functions


WAP_C04: C Lab Program


//reading elements using a pointer into an array and display the values using the array.
#include <stdio.h>

int main() {
    int n, sum = 0, arr[10];

    // Input the number of elements
    printf("Enter the number of elements: ");
    scanf("%d", &n);

    int *ptr = arr; 
   
    printf("Enter the elements:\n");
    for (int i = 0; i < n; i++) {
        // Read element using the pointer
        scanf("%d", ptr + i);  // Pointer arithmetic to read the value
    }

    // Display the values using the array
    printf("The elements are:\n");
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);  // Display elements using the array
    }

    return 0;
}



OUTPUT


Enter the number of elements: 5
Enter the elements:
2 4 6 8 10
The elements are:
2 4 6 8 10


For all 2026 published articles list: click here

...till the next post, bye-bye & take care.

Thursday, March 26, 2026

Write a program that uses functions to perform Multiplication of two matrices || C Lab Program

 WAP_C03: Write a C program that uses functions to perform Multiplication of two matrices.|| Arrays, Pointers and Functions


WAP_C03: C Lab Program


// Multiplication of Two Matrices

#include <stdio.h>

// Function to perform matrix multiplication
void multiplyMatrices(int A[10][10], int B[10][10], int result[10][10], int rowA, int colA, int rowB, int colB) {
    int i,j,k;
    for (i = 0; i < rowA; i++) {
        for (j = 0; j < colB; j++) {
            result[i][j] = 0; // Initializing the result matrix with 0
            for (k = 0; k < colA; k++) {
                result[i][j] += A[i][k] * B[k][j];
            }
        }
    }
}

// Function to input matrix elements
void inputMatrix(int matrix[10][10], int rows, int cols) {
    int i,j;
    printf("Enter elements of the matrix (%dx%d):\n", rows, cols);
    for (i = 0; i < rows; i++) {
        for (j = 0; j < cols; j++) {
            scanf("%d", &matrix[i][j]);
        }
    }
}

// Function to display a matrix
void displayMatrix(int matrix[10][10], int rows, int cols) {
    int i,j;
    for (i = 0; i < rows; i++) {
        for (j = 0; j < cols; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }
}

int main() {
    int A[10][10], B[10][10], result[10][10];
    int rowA, colA, rowB, colB;

    // Input matrix dimensions
    printf("Enter the number of rows and columns for Matrix A: ");
    scanf("%d %d", &rowA, &colA);

    printf("Enter the number of rows and columns for Matrix B: ");
    scanf("%d %d", &rowB, &colB);

    // Check if multiplication is possible (columns of A must be equal to rows of B)
    if (colA != rowB) {
        printf("Matrix multiplication is not possible. The number of columns of A must be equal to the number of rows of B.\n");
        return 1; // Exit the program if multiplication is not possible
    }

    // Input matrices A and B
    printf("Input matrix A:\n");
    inputMatrix(A, rowA, colA);

    printf("Input matrix B:\n");
    inputMatrix(B, rowB, colB);

    // Perform matrix multiplication
    multiplyMatrices(A, B, result, rowA, colA, rowB, colB);

    // Display the result
    printf("\nMatrix A:\n");
    displayMatrix(A, rowA, colA);

    printf("\nMatrix B:\n");
    displayMatrix(B, rowB, colB);

    printf("\nResultant Matrix (A * B):\n");
    displayMatrix(result, rowA, colB);

    return 0;
}



OUTPUT


Enter the number of rows and columns for Matrix A: 3 3
Enter the number of rows and columns for Matrix B: 3 3
Input matrix A:
Enter elements of the matrix (3x3):
1 2 3
4 5 6
7 8 9
Input matrix B:
Enter elements of the matrix (3x3):
1 0 0
0 1 0
0 0 1

Matrix A:
1  2  3
4  5  6
7  8  9

Matrix B:
1  0  0
0  1  0
0  0  1

Resultant Matrix (A * B):
1  2  3
4  5  6
7  8  9


For all 2026 published articles list: click here

...till the next post, bye-bye & take care.