CS Electrical And Electronics
@cselectricalandelectronics
All PostsC_languageCareerProgramming

C Program On Arrays With Output For Placement Exams

Hello guys, welcome back to my blog. In this article, I will share some C program on arrays, I will share codes with outputs and this article will be helpful for those who are preparing for interviews.

Subscribe our website for more updates. You can also catch me @ Instagram – Chetan Shidling. 

Also, read:

  1. What Is Business Analytics, Program Outcome, Career Prospects.
  2. Data Science Work In Daily Life, What Data Science Engineers Do.
  3. Python Programming For Data Science And Machine Learning.

C Program On Arrays

01. Write a C program for addition and subtraction of two matrix?

#include <stdio.h>
int main()
{
    int m, n, c, d, first[10][10], second[10][10], sum[10][10];
    printf("Enter the number of rows and columns in the matrix\n");
    scanf("%d %d",&m,&n);
    printf("Enter the elements of the 1st matrix: \n");

    for(c=0; c<m; c++)
        for(d=0; d<n; d++)
            scanf("%d", &first[c][d]);


    printf("Enter the elements of the 2nd matrix :\n");

    for(c=0; c<m; c++)
        for(d=0; d<n; d++)
            scanf("%d", &second[c][d]);

    for(c=0; c<m; c++)
        for(d=0; d<n; d++)
            sum[c][d] = first[c][d] + second[c][d];

    printf("Sum of entered matrix is: \n");

    for(c=0; c<m; c++)
    {
        for(d=0;d<n;d++)
            printf("%d\t",sum[c][d]);

        printf("\n");
    }

    for(c=0; c<m; c++)
        for(d=0; d<n; d++)
            sum[c][d] = first[c][d] - second[c][d];

    printf("Difference b/t entered matrix: \n");
    for(c=0; c<m; c++)
    {
        for(d=0; d<n; d++)
            printf("%d\t", sum[c][d]);

        printf("\n");
    }

    return 0;
}

Output:

Enter the number of rows and columns in the matrix
3
3
Enter the elements of the 1st matrix:
2
3
5
1
2
3
4
5
6
Enter the elements of the 2nd matrix :
6
4
3
1
2
9
8
2
3
Sum of entered matrix is:
8       7       8
2       4       12
12      7       9
Difference b/t entered matrix:
-4      -1      2
0       0       -6
-4      3       3

02. Write a C Program on addition of two matrix?

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int m, n, c, d, first[10][10], second[10][10], sum[10][10];
    printf("Enter the number of columns and rows of matrix: \n");
    scanf("%d%d",&m, &n);
    printf("Enter the elements of 1st matrix :\n");

    for(c=0; c<m; c++)
    {
        for(d=0; d<n; d++)
        {
            scanf("%d",& first[c][d]);
        }
    }

    printf("Enter the elements of 2nd matrix: \n");

    for(c=0; c<m; c++)
    {
        for(d=0; d<n; d++)
        {
            scanf("%d",& second[c][d]);
        }
    }

    printf("Sum of entered matrix: \n");

    for(c=0; c<m; c++)
    {
        for(d=0; d<n; d++)
        {
            sum[c][d] = first[c][d] + second[c][d];
            printf("%d\t", sum[c][d]);
        }
        printf("\n");
    }
    return 0;
}

Output:

Enter the number of columns and rows of matrix:
3
3
Enter the elements of 1st matrix :
2
3
4
1
3
5
7
4
2
Enter the elements of 2nd matrix:
3
45
1
0
7
6
4
2
5
Sum of entered matrix:
5       48      5
1       10      11
11      6       7

02. Write a C program to store elements in array and display in ascending order?

#include <stdio.h>

int main()
{

    int i, j, a, n, number[30];

    printf("Enter the value of N \n");
    scanf("%d", &n);
    printf("Enter the numbers \n");

    for (i = 0; i < n; ++i)
    {
        scanf("%d", &number[i]);
    }

    for (i = 0; i < n; ++i)
    {
        for (j = i + 1; j < n; ++j)
        {
            if (number[i] > number[j])
            {
                a = number[i];
                number[i] = number[j];
                number[j] = a;
            }
        }
    }

    printf("The numbers arranged in an ascending order are given : \n");

    for (i = 0; i < n; ++i)
    {
        printf("%d\n", number[i]);
    }
    return 0;
}

Output:

Enter the value of N
8
Enter the numbers
2
34
65
12
98
43
32
76
The numbers arranged in an ascending order are given :
2
12
32
34
43
65
76
98

03. Write a C program to check matrix is a sparse matrix or not?

#include <stdio.h>

int main()
{
    static int array[10][10];
    int i, j, m, n;
    int counter = 0;

    printf("Enter the order of the matix : ");
    scanf("%d %d", &m, &n);
    printf("Enter the co-efficients of the matix \n");
    for (i = 0; i < m; ++i)
    {
        for (j = 0; j < n; ++j)
        {
            scanf("%d", &array[i][j]);
            if (array[i][j] == 0)
            {
                ++counter;
            }
        }
    }


    if (counter > ((m * n) / 2))
    {
        printf("The matrix is sparse matrix \n");
    }
    else
    {
        printf("The matrix is not a sparse matrix \n");
    }

    printf("There are %d number of zeros", counter);
    return 0;
}

Output:

Enter the order of the matix : 3
3
Enter the co-efficients of the matix
1
0
0
0
1
0
0
0
1
The matrix is sparse matrix

04. Write a C program to delete element in an array?

#include <stdio.h>

int main()
{
    int array[100], position, c, n;

    printf("Enter number of elements in array :\n");
    scanf("%d", &n);

    printf("Enter %d elements :\n", n);

    for (c = 0; c < n; c++)
        scanf("%d", &array[c]);

    printf("Enter the location where you wish to delete element :\n");
    scanf("%d", &position);

    if (position >= n + 1)
        printf("Deletion not possible.\n");
    else
    {
        for (c = position - 1; c < n - 1; c++)
            array[c] = array[c + 1];

        printf("Resultant array is\n");

        for (c = 0; c < n - 1; c++)
            printf("%d\n", array[c]);
    }

    return 0;
}

Output:

Enter number of elements in array :
8
Enter 8 elements :
23
1
76
44
34
32
99
73
Enter the location where you wish to delete element :
4
Resultant array is
23
1
76
34
32
99
73

05. Write a C program to delete user given number or particular value in an array?

#include <stdio.h>
int main()
{
    int vectorx[10];
    int i, n, pos, element, found = 0;

    printf("Enter how many elements: \n");
    scanf("%d", &n);
    printf("Enter the elements: \n");

    for (i = 0; i < n; i++)
    {
        scanf("%d", &vectorx[i]);
    }

    printf("Input array elements are :\n");

    for (i = 0; i < n; i++)
    {
        printf("%d\n", vectorx[i]);
    }

    printf("Enter the element to be deleted :\n");

    scanf("%d", &element);

    for (i = 0; i < n; i++)
    {
        if (vectorx[i] == element)
        {

            found = 1;
            pos = i;
            break;

        }
    }

    if (found == 1)
    {
        for (i = pos; i < n - 1; i++)
        {

            vectorx[i] = vectorx[i + 1];

        }

        printf("The resultant vector is \n");

        for (i = 0; i < n - 1; i++)
        {

            printf("%d\n", vectorx[i]);

        }
    }
    else
    {
        printf("Element %d is not found in the vector: \n", element);
    }
    return 0;
}

Output:

Enter how many elements:
8
Enter the elements:
4
33
23
78
99
88
43
29
Input array elements are :
4
33
23
78
99
88
43
29
Enter the element to be deleted :
99
The resultant vector is
4
33
23
78
88
43
29

06. Write a C program to find determinant of 2 * 2 matrix?

#include<stdio.h>

int main()
{
    int a[2][2], i, j;
    long determinant;

    printf("Enter the 4 elements of matrix :\n");
    for (i = 0; i < 2; i++)
    {
        for (j = 0; j < 2; j++)
        {
            scanf("%d", &a[i][j]);
        }
    }

    printf("\nThe matrix is\n");

    for (i = 0; i < 2; i++)
    {
        printf("\n");
        for (j = 0; j < 2; j++)
        {
            printf("%d\t", a[i][j]);
        }
    }

    determinant = a[0][0] * a[1][1] - a[1][0] * a[0][1];

    printf("\nDeterminant of 2X2 matrix: %ld", determinant);

    return 0;
}

Output:

Enter the 4 elements of matrix :
4
6
3
2

The matrix is

4       6
3       2
Determinant of 2X2 matrix: -10

07. Write a C program to find determinant of 3*3 matrix?

#include<stdio.h>

int main()
{

    int a[3][3], i, j;

    long determinant;

    printf("Enter the 9 elements in the matrix :\n");
    for (i = 0; i < 3; i++)
        for (j = 0; j < 3; j++)
            scanf("%d", &a[i][j]);

    printf("\nThe matrix is\n");
    for (i = 0; i < 3; i++)
    {
        printf("\n");
        for (j = 0; j < 3; j++)
            printf("%d\t", a[i][j]);
    }

    determinant = a[0][0] * ((a[1][1] * a[2][2]) - (a[2][1] * a[1][2]))
                  - a[0][1] * (a[1][0] * a[2][2] - a[2][0] * a[1][2])
                  + a[0][2] * (a[1][0] * a[2][1] - a[2][0] * a[1][1]);

    printf("\nThe determinant of 3X3 matrix: %d", determinant);

    return 0;
}

Output:

Enter the 9 elements of matrix :
4
5
6
3
9
8
1
2
6

The matrix is

4       5       6
3       9       8
1       2       6
Determinant of 3X3 matrix: 84

08. Write a C program to find smallest and largest element in an arrray?

#include<stdio.h>

int main()
{
    int a[50], size, i, big, small;

    printf("\nEnter the size of the array: ");
    scanf("%d", &size);

    printf("\nEnter %d elements in to the array: ", size);
    for (i = 0; i < size; i++)
    {
        scanf("%d", &a[i]);
    }

    big = a[0];
    for (i = 1; i < size; i++)
    {
        if (big < a[i])
        {
            big = a[i];
        }
    }
    printf("Largest element: %d", big);

    small = a[0];
    for (i = 1; i < size; i++)
    {
        if (small > a[i])
        {
            small = a[i];
        }
    }
    printf("\nSmallest element: %d", small);

    return 0;
}

Output:

Enter the size of the array: 8

Enter 8 elements in to the array: 3
4
56
12
99
67
53
76
Largest element: 99
Smallest element: 3

09. Write a C program to find matrix is identity matrix or not?

#include <stdio.h>

int main()
{
    int a[10][10];
    int i, j, row, column, flag = 1;

    printf("Enter the dimensions of the matrix A : ");
    scanf("%d %d", &row, &column);
    printf("Enter the elements of matrix A \n");

    for (i = 0; i < row; i++)
    {
        for (j = 0; j < column; j++)
        {
            scanf("%d", &a[i][j]);
        }
    }
    printf("MATRIX A is \n");
    for (i = 0; i < row; i++)
    {
        for (j = 0; j < column; j++)
        {
            printf("%3d", a[i][j]);
        }
        printf("\n");
    }

    for (i = 0; i < row; i++)
    {
        for (j = 0; j < column; j++)
        {
            if (a[i][j] != 1 && a[j][i] != 0)
            {
                flag = 0;
                break;
            }
        }
    }
    if (flag == 1)
        printf("It is identity matrix \n");
    else
        printf("It is not a identity matrix \n");
}

Output:

Enter the dimensions of the matrix A : 3
3
Enter the elements of matrix A
3
4
5
9
1
6
4
3
2
MATRIX A is
  3  4  5
  9  1  6
  4  3  2
It is not a identity matrix


Enter the dimensions of the matrix A : 3
3
Enter the elements of matrix A
1
0
0
0
1
0
0
0
1
MATRIX A is
  1  0  0
  0  1  0
  0  0  1
It is identity matrix

10. Write a C program to implement Queue using array?

#include <stdio.h>
#include <stdlib.h>

#define MAX 50

int queue_array[MAX];
int rear = -1;
int front = -1;

void insert()
{

    int add_item;

    if (rear == MAX - 1)
    {

        printf("Queue Overflow \n");

    }
    else
    {

        if (front == -1)
        {
            // If queue is initially empty
            front = 0;

        }

        printf("Inset the element in queue : ");

        scanf("%d", &add_item);

        rear = rear + 1;
        queue_array[rear] = add_item;

    }

}


void display()
{

    int i;

    if (front == -1)
    {
        printf("Queue is empty \n");
    }
    else
    {

        printf("Queue is : \n");

        for (i = front; i <= rear; i++)
        {
            printf("%d ", queue_array[i]);
        }

        printf("\n");

    }

}


void deleteElement()
{

    if (front == -1 || front > rear)
    {
        printf("Queue Underflow \n");
        return;
    }
    else
    {
        printf("Element deleted from queue is : %d\n", queue_array[front]);
        front = front + 1;
    }

}

int main()
{

    int choice;

    printf("1.Insert element to queue \n");
    printf("2.Delete element from queue \n");
    printf("3.Display all elements of queue \n");
    printf("4.Quit \n");

    while (1)
    {
        printf("Enter your choice : ");

        scanf("%d", &choice);

        switch (choice)
        {

            case 1:
                insert();
                break;
            case 2:
                deleteElement();
                break;
            case 3:
                display();
                break;
            case 4:
                exit(1);
            default:
                printf("Wrong choice \n");
        }
    }
}

Output:

1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 1
Inset the element in queue : 2
Enter your choice : 1
Inset the element in queue : 7
Enter your choice : 1
Inset the element in queue : 9
Enter your choice : 3
Queue is :
2 7 9
Enter your choice : 2
Element deleted from queue is : 2
Enter your choice : 3
Queue is :
7 9
Enter your choice : 1
Inset the element in queue : 2
Enter your choice : 3
Queue is :
7 9 2
Enter your choice : 2
Element deleted from queue is : 7
Enter your choice : 3
Queue is :
9 2
Enter your choice :

11. Write a C program to implement stack using array?

#include <stdio.h>

#define MAX 5
int top, status;

// PUSH FUNCTION
void push(int stack[], int item)
{
    if (top == (MAX - 1))
        status = 0;
    else
    {
        status = 1;
        ++top;
        stack[top] = item;
    }
}

// POP FUNCTION
int pop(int stack[])
{
    int ret;
    if (top == -1)
    {
        ret = 0;
        status = 0;
    }
    else
    {
        status = 1;
        ret = stack[top];
        --top;
    }
    return ret;
}

// FUNCTION TO DISPLAY STACK
void display(int stack[])
{
    int i;
    printf("\nThe Stack is: ");
    if (top == -1)
        printf("empty");
    else
    {
        for (i = top; i >= 0; --i)
            printf("\n--------\n|%3d   |\n--------", stack[i]);
    }
    printf("\n");
}

int main()
{
    int stack[MAX], item;
    int ch;
    top = -1;

    printf("\nMAIN MENU");
    printf("\n1.PUSH (Insert) in the Stack");
    printf("\n2.POP  (Delete) from the Stack");
    printf("\n3.Exit (End the Execution)");

    do
    {
        do
        {

            printf("\nEnter Your Choice: ");
            scanf("%d", &ch);

            if (ch < 1 || ch > 3)
                printf("\nInvalid Choice, Please try again");

        } while (ch < 1 || ch > 3);

        switch (ch)
        {
            case 1:
                printf("\nEnter the Element to be pushed : ");
                scanf("%d", &item);
                printf("%d", item);
                push(stack, item);
                if (status)
                {
                    printf("\nAfter Pushing ");
                    display(stack);
                    if (top == (MAX - 1))
                        printf("\nThe Stack is Full");
                }
                else
                    printf("\nStack overflow on Push");
                break;
            case 2:
                item = pop(stack);
                if (status)
                {
                    printf("\nThe Popped item is %d.  After Popping: ", item);
                    display(stack);
                }
                else
                    printf("\nStack underflow on Pop");
                break;

            default:
                printf("\nEND OF EXECUTION");
        }
    } while (ch != 3);
    return 0;
}

Output:

MAIN MENU
1.PUSH (Insert) in the Stack
2.POP  (Delete) from the Stack
3.Exit (End the Execution)
Enter Your Choice: 1

Enter the Element to be pushed : 34
34
After Pushing
The Stack is:
--------
| 34   |
--------

Enter Your Choice: 1

Enter the Element to be pushed : 88
88
After Pushing
The Stack is:
--------
| 88   |
--------
--------
| 34   |
--------

Enter Your Choice: 1

Enter the Element to be pushed : 43
43
After Pushing
The Stack is:
--------
| 43   |
--------
--------
| 88   |
--------
--------
| 34   |
--------

Enter Your Choice: 2

The Popped item is 43.  After Popping:
The Stack is:
--------
| 88   |
--------
--------
| 34   |
--------

Enter Your Choice: 3

END OF EXECUTION

12. Write a C program to insert element in an array?

#include <stdio.h>

int main()
{
    int array[100], position, c, n, value;

    printf("Enter number of elements in array : ");
    scanf("%d", &n);

    printf("Enter %d elements : ", n);

    for (c = 0; c < n; c++)
    {
        scanf("%d", &array[c]);
    }

    printf("Enter the location where you want to insert an element : ");
    scanf("%d", &position);

    printf("Enter the value to insert : ");
    scanf("%d", &value);

    for (c = n - 1; c >= position - 1; c--)
    {
        array[c + 1] = array[c];
    }

    array[position - 1] = value;

    printf("Resultant array is\t");

    for (c = 0; c <= n; c++)
    {
        printf("%d\t", array[c]);
    }

    return 0;
}

Output:

Enter 8 elements : 34
23
98
56
45
12
88
44
Enter the location where you wish to insert an element : 4
Enter the value to insert : 29
Resultant array is      34      23      98      29      56      45      12      88      44

13. Write a C program to inverse the matrix?

#include<stdio.h>

void reduction(float a[][6], int size, int pivot, int col)
{
    int i, j;
    float factor;
    factor = a[pivot][col];

    for (i = 0; i < 2 * size; i++)
    {
        a[pivot][i] /= factor;
    }

    for (i = 0; i < size; i++)
    {
        if (i != pivot)
        {
            factor = a[i][col];
            for (j = 0; j < 2 * size; j++)
            {
                a[i][j] = a[i][j] - a[pivot][j] * factor;
            }
        }
    }
}

int main()
{
    float matrix[3][6];
    int i, j;

    for (i = 0; i < 3; i++)
    {
        for (j = 0; j < 6; j++)
        {
            if (j == i + 3)
            {
                matrix[i][j] = 1;
            }
            else
            {
                matrix[i][j] = 0;
            }
        }
    }

    printf("\nEnter a 3 X 3 Matrix :");
    for (i = 0; i < 3; i++)
    {
        for (j = 0; j < 3; j++)
        {
            scanf("%f", &matrix[i][j]);
        }
    }

    for (i = 0; i < 3; i++)
    {
        reduction(matrix, 3, i, i);
    }

    printf("\nInvers Matrix");
    for (i = 0; i < 3; i++)
    {
        printf("\n");
        for (j = 0; j < 3; j++)
        {
            printf("%8.3f", matrix[i][j + 3]);
        }
    }

    return 0;
}

Output:

Enter a 3 X 3 Matrix :43
23
12
77
65
45
98
12
10

Invers Matrix
   0.005  -0.004   0.011
   0.158  -0.032  -0.044
  -0.236   0.075   0.044

14. C code for magic matrix?

#include<stdio.h>

int main()
{
    int i, j = 1, l, n;
    int mm[50][50], x, y, z;
    int c = 0;

    label:
    printf("Enter the Order of Magic Matrix(Odd) : ");
    scanf("%d", &n);
    printf("\n");
    if (n % 2 == 0)
    {
        printf("Enter Odd Numbers Only\n\n");
        goto label;
    }

    //find the middle column
    l = (n + 1) / 2;

    //set all the elements to be used to 0
    for (x = 1; x <= n; x++)
    {
        for (y = 1; y <= n; y++)
        {
            mm[x][y] = NULL;
        }
    }

    for (i = 1; i <= (n * n); i++)
    {
        if (mm[j][l] == NULL)
        {
            mm[j][l] = i;
        }
        else
        {
            j = j + 2;
            l--;
            if (j == n + 2)
                j = 2;
            if (l == 0)
                l = n;
            if (j == 0)
                j = n;
            if (l == n + 1)
                l = 1;
            mm[j][l] = i;
        }
        j--;
        l++;
        if (j == 0)
            j = n;
        if (l == n + 1)
            l = 1;
    }

    for (x = 1; x <= n; x++)
    {
        for (y = 1; y <= n; y++)
        {
            z = mm[x][y];
            while (z > 0)
            {
                z = z / 10;
                c++;
            }
            if (c == 1)
                printf(" 0%d", mm[x][y]);
            else
                printf(" %d", mm[x][y]);
            if (y % n == 0)
                printf("\n\n");
            c = 0;
        }
    }
    return 0;
}

Output:

Enter the Order of Magic Matrix(Odd) : 5

 17 24 01 08 15

 23 05 07 14 16

 04 06 13 20 22

 10 12 19 21 03

 11 18 25 02 09


Enter the Order of Magic Matrix(Odd) : 6

Enter Odd Numbers Only


Enter the Order of Magic Matrix(Odd) : 3

 08 01 06

 03 05 07

 04 09 02

15. Write a C program for matrix multiplication?

#include <stdio.h>

int main()
{
    int m, n, p, q, c, d, k, sum = 0;
    int first[10][10], second[10][10], multiply[10][10];

    printf("Enter the no of rows and columns of 1st matrix :\n");
    scanf("%d%d", &m, &n);
    printf("Enter the elements of 1st matrix\n");

    for (c = 0; c < m; c++)
        for (d = 0; d < n; d++)
            scanf("%d", &first[c][d]);

    printf("Enter the no of rows and columns of 2nd matrix\n");
    scanf("%d%d", &p, &q);

    if (n != p)
        printf("Matrices with entered orders can not be multiplied with each other.\n");
    else
    {
        printf("Enter the elements of 2nd matrix\n");

        for (c = 0; c < p; c++)
            for (d = 0; d < q; d++)
                scanf("%d", &second[c][d]);

        for (c = 0; c < m; c++)
        {
            for (d = 0; d < q; d++)
            {
                for (k = 0; k < p; k++)
                {
                    sum = sum + first[c][k] * second[k][d];
                }

                multiply[c][d] = sum;
                sum = 0;
            }
        }

        printf("Product of entered matrices:-\n");

        for (c = 0; c < m; c++)
        {
            for (d = 0; d < q; d++)
                printf("%d\t", multiply[c][d]);

            printf("\n");
        }
    }

    return 0;
}

Output:

Enter the no of rows and columns of 1st matrix :
3
3
Enter the elements of 1st matrix
2
3
4
3
1
7
9
5
2
Enter the no of rows and columns of 2nd matrix
3
3
Enter the elements of 2nd matrix
1
9
3
7
2
8
5
1
9
Product of entered matrices:-
43      28      66
45      36      80
54      93      85


Enter the no of rows and columns of 1st matrix :
3
3
Enter the elements of 1st matrix
3
9
1
9
2
7
5
1
4
Enter the no of rows and columns of 2nd matrix
4
5
Matrices with entered orders can not be multiplied with each other.

This was about the C program on arrays. I hope this article may help you all a lot. Thank you for reading.

Also, read:

Author Profile

CS Electrical And ElectronicsChetu
Interest's ~ Engineering | Entrepreneurship | Politics | History | Travelling | Content Writing | Technology | Cooking
Share Now

CS Electrical And Electronics

Interest's ~ Engineering | Entrepreneurship | Politics | History | Travelling | Content Writing | Technology | Cooking