C PROGRAMMING
MULTIPLE CHOICE QUESTION
OLD QUESTION BANK
SAMPLE QUESTION 2080 AND SOLUTION

1-D ARRAY 

1.  C Program to print negative elements in array.

#include <stdio.h>

#define MAX 100 // Maximum array size

int main() {
  int arr[MAX]; // Declare array of MAX_SIZE
  int i, n;

  /* Input size of the array */
  printf("Enter size of the array : ");
  scanf("%d", & n);

  /* Input elements in the array */
  printf("Enter elements in array : ");
  for (i = 0; i < n; i++) {
    scanf("%d", & arr[i]);
  }

  printf("\nAll negative elements in array are : ");
  for (i = 0; i < n; i++) {
    /* If current array element is negative */
    if (arr[i] < 0) {
      printf("%d\t", arr[i]);
    }
  }

  return 0;
}

2. C program to count even and odd elements in array.

#include <stdio.h>

#define MAX_SIZE 100 //Maximum size of the array

int main() {
  int arr[MAX_SIZE];
  int i, size, even, odd;

  /* Input size of the array */
  printf("Enter size of the array: ");
  scanf("%d", & size);

  /* Input array elements */
  printf("Enter %d elements in array: ", size);
  for (i = 0; i < size; i++) 
  {
    scanf("%d", & arr[i]);
  }

  even = 0;
  odd = 0;

  for (i = 0; i < size; i++) 
   {
    if (arr[i] % 2 == 0) 
    {
      even++;
    } else {
      odd++;
    }
  }

  printf("Total even elements: %d\n", even);
  printf("Total odd elements: %d", odd);

  return 0;
}

3. C program to find largest number in array.

#include <stdio.h>

int main() {
  int n;
  double arr[100];
  printf("Enter the number of elements (1 to 100): ");
  scanf("%d", & n);

  for (int i = 0; i < n; ++i) {
    printf("Enter number%d: ", i + 1);
    scanf("%lf", & arr[i]);
  }

  // storing the largest number to arr[0]
  for (int i = 1; i < n; ++i) {
    if (arr[0] < arr[i]) {
      arr[0] = arr[i];
    }
  }

  printf("Largest element = %.2lf", arr[0]);

  return 0;
}

4. C program to copy all elements of one array to another

#include <stdio.h>

#define MAX_SIZE 100

int main() {
  int source[MAX_SIZE], dest[MAX_SIZE];
  int i, size;

  /* Input size of the array */
  printf("Enter the size of the array : ");
  scanf("%d", & size);

  /* Input array elements */
  printf("Enter elements of source array : ");
  for (i = 0; i < size; i++) {
    scanf("%d", & source[i]);
  }

  for (i = 0; i < size; i++) {
    dest[i] = source[i];
  }

  /* 
   * Print all elements of source array
   */
  printf("\nElements of source array are : ");
  for (i = 0; i < size; i++) {
    printf("%d\t", source[i]);
  }

  /*
   * Print all elements of dest array
   */
  printf("\nElements of dest array are : ");
  for (i = 0; i < size; i++) {
    printf("%d\t", dest[i]);
  }

  return 0;
}

5. C Program to find maximum and minimum element in array

#include <stdio.h>

#define MAX_SIZE 100 // Maximum array size

int main() {
  int arr[MAX_SIZE];
  int i, max, min, size;

  /* Input size of the array */
  printf("Enter size of the array: ");
  scanf("%d", & size);

  /* Input array elements */
  printf("Enter elements in the array: ");
  for (i = 0; i < size; i++) {
    scanf("%d", & arr[i]);
  }

  /* Assume first element as maximum and minimum */
  max = arr[0];
  min = arr[0];

  for (i = 1; i < size; i++) {
    /* If current element is greater than max */
    if (arr[i] > max) {
      max = arr[i];
    }

    /* If current element is smaller than min */
    if (arr[i] < min) {
      min = arr[i];
    }
  }

  /* Print maximum and minimum element */
  printf("Maximum element = %d\n", max);
  printf("Minimum element = %d", min);

  return 0;
}

6. C program to sort array in ascending or descending order


#include <stdio.h>

#define MAX_SIZE 100 // Maximum array size

int main() {
  int arr[MAX_SIZE];
  int size;
  int i, j, temp;

  /* Input size of array */
  printf("Enter size of array: ");
  scanf("%d", & size);

  /* Input elements in array */
  printf("Enter elements in array: ");
  for (i = 0; i < size; i++) {
    scanf("%d", & arr[i]);
  }

  for (i = 0; i < size; i++) {

    for (j = i + 1; j < size; j++) {

      if (arr[i] > arr[j]) {
        temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
      }
    }
  }

  /* Print the sorted array */
  printf("\nElements of array in ascending order: ");
  for (i = 0; i < size; i++) {
    printf("%d\t", arr[i]);
  }

  return 0;
}

7. C Program to insert element in array

#include <stdio.h>

#define MAX_SIZE 100

int main() {
  int arr[MAX_SIZE];
  int i, size, num, pos;

  /* Input size of the array */
  printf("Enter size of the array : ");
  scanf("%d", & size);

  /* Input elements in array */
  printf("Enter elements in array : ");
  for (i = 0; i < size; i++) {
    scanf("%d", & arr[i]);
  }

  /* Input new element and position to insert */
  printf("Enter element to insert : ");
  scanf("%d", & num);
  printf("Enter the element position : ");
  scanf("%d", & pos);

  /* If position of element is not valid */
  if (pos > size + 1 || pos <= 0) {
    printf("Invalid position! Please enter position between 1 to %d", size);
  } else {
    /* Make room for new array element by shifting to right */
    for (i = size; i >= pos; i--) {
      arr[i] = arr[i - 1];
    }

    /* Insert new element at given position and increment size */
    arr[pos - 1] = num;
    size++;

    /* Print array after insert operation */
    printf("Array elements after insertion : ");
    for (i = 0; i < size; i++) {
      printf("%d\t", arr[i]);
    }
  }

  return 0;
}

8. C Program to print array in reverse.

#include <stdio.h>

#define MAX_SIZE 100 // Defines maximum size of array

int main() {
  int arr[MAX_SIZE];
  int size, i;

  /* Input size of array */
  printf("Enter size of the array: ");
  scanf("%d", & size);

  /* Input array elements */
  printf("Enter elements in array: ");
  for (i = 0; i < size; i++) {
    scanf("%d", & arr[i]);
  }

  /*
   * Print array in reversed order
   */
  printf("\nArray in reverse order: ");
  for (i = size - 1; i >= 0; i--) {
    printf("%d\t", arr[i]);
  }

  return 0;
}

9. C program to merge two sorted array


#include <stdio.h>
#define MAX_SIZE 100      // Maximum size of the array

int main()
{
    int arr1[MAX_SIZE], arr2[MAX_SIZE], mergeArray[MAX_SIZE * 2];
    int size1, size2, mergeSize;
    int index1, index2, mergeIndex;
    int i;
     
    /* Input size of first array */
    printf("Enter the size of first array : ");
    scanf("%d", &size1);

    /* Input elements in first array */
    printf("Enter elements in first array : ");
    for(i=0; i<size1; i++)
    {
        scanf("%d", &arr1[i]);
    }

    /* Input size of second array */
    printf("\nEnter the size of second array : ");
    scanf("%d", &size2);

    /* Input elements in second array */
    printf("Enter elements in second array : ");
    for(i=0; i<size2; i++)
    {
        scanf("%d", &arr2[i]);
    }


    mergeSize = size1 + size2;


    /*
     * Merge two array in ascending order 
     */
    index1 = 0;
    index2 = 0;
    for(mergeIndex=0; mergeIndex < mergeSize; mergeIndex++)
    {
        /* 
         * If all elements of one array 
         * is merged to final array
         */
        if(index1 >= size1 || index2 >= size2)
        {
            break;
        }


        if(arr1[index1] < arr2[index2])
        {
            mergeArray[mergeIndex] = arr1[index1];
            index1++;
        }
        else
        {
            mergeArray[mergeIndex] = arr2[index2];
            index2++;
        }
    }

    /*
     * Merge remaining array elements
     */
    while(index1 < size1)
    {
        mergeArray[mergeIndex] = arr1[index1];
        mergeIndex++;
        index1++;
    }
    while(index2 < size2)
    {
        mergeArray[mergeIndex] = arr2[index2];
        mergeIndex++;
        index2++;
    }


    /* 
     * Print merged array
     */
    printf("\nArray merged in ascending order : ");
    for(i=0; i<mergeSize; i++)
    {
        printf("%d\t", mergeArray[i]);
    }

    return 0;
}

10. C Program to count negative elements in array.

#include <stdio.h>

#define MAX_SIZE 100 // Maximum array size

int main() {
  int arr[MAX_SIZE]; // Declares array of size 100
  int i, size, count = 0;

  /* Input size of array */
  printf("Enter size of the array : ");
  scanf("%d", & size);

  /* Input array elements */
  printf("Enter elements in array : ");
  for (i = 0; i < size; i++) {
    scanf("%d", & arr[i]);
  }

  /*
   * Count total negative elements in array
   */
  for (i = 0; i < size; i++) {
    /* Increment count if current array element is negative */
    if (arr[i] < 0) {
      count++;
    }
  }

  printf("\nTotal negative elements in array = %d", count);

  return 0;
}

2-D ARRAY

1. C Program to add two matrices

#include <stdio.h>

#define SIZE 3 // Size of the matrix

int main() {
  int A[SIZE][SIZE]; // Matrix 1
  int B[SIZE][SIZE]; // Matrix 2
  int C[SIZE][SIZE]; // Resultant matrix

  int row, col;

  /* Input elements in first matrix*/
  printf("Enter elements in matrix A of size 3x3: \n");
  for (row = 0; row < SIZE; row++) {
    for (col = 0; col < SIZE; col++) {
      scanf("%d", & A[row][col]);
    }
  }

  /* Input elements in second matrix */
  printf("\nEnter elements in matrix B of size 3x3: \n");
  for (row = 0; row < SIZE; row++) {
    for (col = 0; col < SIZE; col++) {
      scanf("%d", & B[row][col]);
    }
  }

  /*
   * Add both matrices A and B entry wise or element wise
   * and stores result in matrix C
   */
  for (row = 0; row < SIZE; row++) {
    for (col = 0; col < SIZE; col++) {
      /* Cij = Aij + Bij */
      C[row][col] = A[row][col] + B[row][col];
    }
  }

  /* Print the value of resultant matrix C */
  printf("\nSum of matrices A+B = \n");
  for (row = 0; row < SIZE; row++) {
    for (col = 0; col < SIZE; col++) {
      printf("%d ", C[row][col]);
    }
    printf("\n");
  }

  return 0;
}

2. C program to multiply two matrices.


#include <stdio.h>

#define SIZE 3 // Size of the matrix

int main()
{
    int A[SIZE][SIZE]; // Matrix 1 
    int B[SIZE][SIZE]; // Matrix 2
    int C[SIZE][SIZE]; // Resultant matrix
    
    int row, col, i, sum;


    /* Input elements in first matrix from user */
    printf("Enter elements in matrix A of size %dx%d: \n", SIZE, SIZE);
    for(row=0; row<SIZE; row++)
    {
        for(col=0; col<SIZE; col++)
        {
            scanf("%d", &A[row][col]);
        }
    }

    /* Input elements in second matrix from user */
    printf("\nEnter elements in matrix B of size %dx%d: \n", SIZE, SIZE);
    for(row=0; row<SIZE; row++)
    {
        for(col=0; col<SIZE; col++)
        {
            scanf("%d", &B[row][col]);
        }
    }

    /*
     * Multiply both matrices A*B
     */
    for(row=0; row<SIZE; row++)
    {
        for(col=0; col<SIZE; col++)
        {
            sum = 0;
            /*
             * Multiply row of first matrix to column of second matrix
             * and store sum of product of elements in sum.
             */
            for(i=0; i<SIZE; i++)
            {
                sum += A[row][i] * B[i][col];
            }

            C[row][col] = sum;
        }
    }

    /* Print product of the matrices */
    printf("\nProduct of matrix A * B = \n");
    for(row=0; row<SIZE; row++)
    {
        for(col=0; col<SIZE; col++)
        {
            printf("%d ", C[row][col]);
        }
        printf("\n");
    }

    return 0;
}

3. C program to check whether two matrices are equal or not



#include <stdio.h>

#define SIZE 3 // Matrix size

int main()
{
    int A[SIZE][SIZE]; 
    int B[SIZE][SIZE];

    int row, col, isEqual;

    /* Input elements in first matrix from user */
    printf("Enter elements in matrix A of size %dx%d: \n", SIZE, SIZE);
    for(row=0; row<SIZE; row++)
    {
        for(col=0; col<SIZE; col++)
        {
            scanf("%d", &A[row][col]);
        }
    }

    /* Input elements in second matrix from user */
    printf("\nEnter elements in matrix B of size %dx%d: \n");
    for(row=0; row<SIZE; row++)
    {
        for(col=0; col<SIZE; col++)
        {
            scanf("%d", &B[row][col]);
        }
    }

    /* Assumes that the matrices are equal */
    isEqual = 1;

    for(row=0; row<SIZE; row++)
    {
        for(col=0; col<SIZE; col++)
        {
            /*
             * If the corresponding entries of matrices are not equal
             */
            if(A[row][col] != B[row][col])
            {
                isEqual = 0;
                break;
            }
        }
    }

    /*
     * Checks the value of isEqual
     * As per our assumption if isEqual contains 1 means both are equal
     * If it contains 0 means both are not equal
     */
    if(isEqual == 1)
    {
        printf("\nMatrix A is equal to Matrix B");
    }
    else
    {
        printf("\nMatrix A is not equal to Matrix B");
    }

    return 0;
}

4. C program to find sum of main diagonal elements of a matrix


#include <stdio.h>

#define SIZE 3 // Matrix size

int main() {
  int A[SIZE][SIZE];
  int row, col, sum = 0;

  /* Input elements in matrix from user */
  printf("Enter elements in matrix of size %dx%d: \n", SIZE, SIZE);
  for (row = 0; row < SIZE; row++) {
    for (col = 0; col < SIZE; col++) {
      scanf("%d", & A[row][col]);
    }
  }

  /* Find sum of main diagonal elements */
  for (row = 0; row < SIZE; row++) {
    sum = sum + A[row][row];
  }

  printf("\nSum of main diagonal elements = %d", sum);

  return 0;
}

5. C program to find the sum of opposite diagonal elements of a matrix


#include <stdio.h>

#define SIZE 3 // Matrix size

int main()
{
    int A[SIZE][SIZE];
    int row, col, sum = 0;

    /* Input elements in matrix from user */
    printf("Enter elements in matrix of size %dx%d: \n");
    for(row=0; row<SIZE; row++)
    {
        for(col=0; col<SIZE; col++)
        {
            scanf("%d", &A[row][col]);
        }
    }

    /* Find sum of minor diagonal elements */
    for(row=0; row<SIZE; row++)
    {
        for(col=0; col<SIZE; col++)
        {
            /*
             * If it is minor diagonal of matrix
             * Minor diagonal: i+j == N + 1
             * Since array elements starts from 0 hence i+j == (N + 1)-2
             */
            if(row+col == ((SIZE+1)-2))
            {
                sum += A[row][col];
            }
        }
    }

    printf("\nSum of minor diagonal elements = %d", sum);

    return 0;
}

6. C program to find sum of each row and columns of a matrix

#include <stdio.h>

#define SIZE 3 // Matrix size

int main() {
  int A[SIZE][SIZE];
  int row, col, sum = 0;

  /* Input elements in matrix from user */
  printf("Enter elements in matrix of size %dx%d: \n", SIZE, SIZE);
  for (row = 0; row < SIZE; row++) {
    for (col = 0; col < SIZE; col++) {
      scanf("%d", & A[row][col]);
    }
  }

  /* Calculate sum of elements of each row of matrix */
  for (row = 0; row < SIZE; row++) {
    sum = 0;
    for (col = 0; col < SIZE; col++) {
      sum += A[row][col];
    }

    printf("Sum of elements of Row %d = %d\n", row + 1, sum);
  }

  /* Find sum of elements of each columns of matrix */
  for (row = 0; row < SIZE; row++) {
    sum = 0;
    for (col = 0; col < SIZE; col++) {
      sum += A[col][row];
    }

    printf("Sum of elements of Column %d = %d\n", row + 1, sum);
  }

  return 0;
}

7. C program to interchange diagonals of a matrix


#include <stdio.h>
#define MAX_ROWS 3
#define MAX_COLS 3

int main()
{
    int A[MAX_ROWS][MAX_COLS];
    int row, col, size, temp;
 
    /* Input elements in matrix from user */
    printf("Enter elements in matrix of size %dx%d: \n", MAX_ROWS, MAX_COLS);
    for(row=0; row<MAX_ROWS; row++)
    {
        for(col=0; col<MAX_COLS; col++)
        {
            scanf("%d", &A[row][col]);
        }
    }

    size = (MAX_ROWS < MAX_COLS) ? MAX_ROWS : MAX_COLS;
 
    /*
     * Interchange diagonal of the matrix
     */
    for(row=0; row<size; row++)
    {
        col = row;
 
        temp = A[row][col];
        A[row][col] = A[row][(size-col) - 1];
        A[row][(size-col) - 1] = temp;
    }
 
    /*
     * Print the interchanged diagonals matrix
     */
    printf("\nMatrix after diagonals interchanged: \n");
    for(row=0; row<MAX_ROWS; row++)
    {
        for(col=0; col<MAX_COLS; col++)
        {
            printf("%d ", A[row][col]);
        }
 
        printf("\n");
    }
 
    return 0;
}

8. C program to multiply two matrix using function. Use separate function to read, multiply and display the product. 

#include <stdio.h>

void read(int [][10], int [][10], int r1,int c1, int r2, int c2);
void multiplication(int [][10],int [][10],int [][10],int r1,int c1,int r2,int c2);
void display(int [][10], int r1, int c2);

int main()
{
    int a[10][10], b[10][10], mult[10][10], r1, c1, r2, c2, i, j, k;

  do
    {
        printf("\nMake the column of the first matrix equal to the row of the second.\n");
        printf("\nEnter rows and column for first matrix: ");
        scanf("%d %d", &r1, &c1);
        printf("Enter rows and column for second matrix: ");
        scanf("%d %d",&r2, &c2);
    } while (c1 != r2);

    read(a,b,r1,c1,r2,c2);
    multiplication(a,b,mult,r1,c1,r2,c2);
    display(mult,r1,c2);
    return 0;
}

//This matrix takes the data of matrices.
void read(int a[][10], int b[][10], int r1,int c1, int r2, int c2)
{
    int i,j;
    printf("\nEnter elements of matrix 1:\n");
    for(i=0; i<r1; ++i)
        for(j=0; j<c1; ++j)
        {
            printf("Enter elements a%d%d: ",i+1,j+1);
            scanf("%d",&a[i][j]);
        }

    printf("\nEnter elements of matrix 2:\n");
    for(i=0; i<r2; ++i)
        for(j=0; j<c2; ++j)
        {
            printf("Enter elements b%d%d: ",i+1,j+1);
            scanf("%d",&b[i][j]);
        }
}

//This function multiplies the entered matrices.
void multiplication(int a[][10],int b[][10],int mult[][10],int r1,int c1,int r2,int c2)
{
    int i,j,k;
    for (int i = 0; i < r1; i++)
    {
        for (int j = 0; j < c2; j++)
        {
            mult[i][j] = 0;

            // Multiplying i’th row with j’th column
            for (int k = 0; k < c1; k++)
            {
                mult[i][j] += a[i][k] * b[k][j];
            }
        }    
        }   
   
}

//This function displays the final matrix after multiplication.
void display(int mult[][10], int r1, int c2)
{
    int i, j;
    printf("\nThe product of the entered matrices is:\n");
    for(i=0; i<r1; ++i)
        for(j=0; j<c2; ++j)
        {
            printf("%d  ",mult[i][j]);
            if(j==c2-1)
                printf("\n\n");
        }
}