0% found this document useful (0 votes)
35 views

C Programs

The document contains programs for matrix addition, transpose of a matrix, matrix multiplication, creating an array of integers using switch case, and merging two arrays into one array. The programs demonstrate basic operations on arrays and matrices in C programming language.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

C Programs

The document contains programs for matrix addition, transpose of a matrix, matrix multiplication, creating an array of integers using switch case, and merging two arrays into one array. The programs demonstrate basic operations on arrays and matrices in C programming language.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Program for matrix addition

#include <stdio.h>

int main()

int a[10][10],b[10][10],row,col,c[10][10];

int i,j;

printf("Enter the no. of rows : ");

scanf("%d",&row);

printf("Enter the no. of columns : ");

scanf("%d",&col);

printf("Enter the elements of a : ");

for(i=0;i<row;i++)

for(j=0;j<col;j++)

printf("\n a[%d][%d] : ",i,j);

scanf("%d",&a[i][j]);

printf("Array A : \n");

for(i=0;i<row;i++)

for(j=0;j<col;j++)

printf("\t%d ",a[i][j]);

printf("\n");

printf("Enter the elements of b : ");

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

for(j=0;j<col;j++)

printf("\n b[%d][%d] : ",i,j);

scanf("%d",&b[i][j]);

printf("Array B : \n");

for(i=0;i<row;i++)

for(j=0;j<col;j++)

printf("\t%d",b[i][j]);

printf("\n");

printf("\n Array C : \n");

for(i=0;i<row;i++)

for(j=0;j<col;j++)

c[i][j]=a[i][j]+b[i][j];

printf("\t%d",c[i][j]);

printf("\n");

printf("\t\t\t PRIYANSHU KUMAR");

return 0;

}
Program for transpose of matrix
#include <stdio.h>

int main()

int a[10][10],row,col;

int i,j;

printf("Enter the no. of rows : ");

scanf("%d",&row);

printf("Enter the no. of columns : ");

scanf("%d",&col);

printf("Enter the elements of a : ");

for(i=0;i<row;i++)

for(j=0;j<col;j++)

printf("\n a[%d][%d] : ",i,j);

scanf("%d",&a[i][j]);

printf("Array A : \n");

for(i=0;i<row;i++)

for(j=0;j<col;j++)

printf("\t%d ",a[i][j]);

printf("\n");

printf("After transpose : \n\n");

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

for(j=0;j<row;j++)

printf("\t%d",a[j][i]);

printf("\n");

printf("\t\t\t PRIYANSHU KUMAR");

return 0;

Program for matrix multiplication

#include <stdio.h>

void get(int matrix[10][10], int row,int col)

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

for(int i=0;i<row;i++)

for( int j=0;j<col;j++)

printf("Element : ");

scanf("%d",&matrix[i][j]);

void multiply(int a[10][10], int b[10][10],int c[10][10],int r1,int c1,int r2,int c2)

{
for (int i = 0; i < r1; ++i) {

for (int j = 0; j < c2; ++j) {

c[i][j] = 0;

for(int i=0;i<r1;i++)

for(int j=0;j<c2;j++)

for(int k=0;k<c1;k++)

c[i][j]+= a[i][k]*b[k][j];

void display(int c[10][10],int r1,int c2)

printf("Resultant MATRIX : \n");

for(int i=0;i<r1;i++)

for(int j=0;j<c2;j++)

printf("\t %d",c[i][j]);

printf("\n");

int main()

int a[10][10], b[10][10],r1,c1,r2,c2,c[10][10];


printf("Enter the no. of row of a matrix : ");

scanf("%d",&r1);

printf("Enter the no. of columns of a matrix : ");

scanf("%d",&c1);

printf("Enter the no. of row of b matrix : ");

scanf("%d",&r2);

printf("Enter the no. of columns of b matrix : ");

scanf("%d",&c2);

if(c1!=r2)

printf("\n \nERROR!! Row of first matrix should be equal to column of another matrix to perform
the multiplication... ");

printf("Enter the no. of row of a matrix : ");

scanf("%d",&r1);

printf("Enter the no. of columns of a matrix : ");

scanf("%d",&c1);

printf("Enter the no. of row of b matrix : ");

scanf("%d",&r2);

printf("Enter the no. of columns of b matrix : ");

scanf("%d",&c2);

get(a,r1,r2);

get(b,r2,c2);

multiply(a,b,c,r1,c1,r2,c2);

display(c,r1,c2);

printf("\t\t\t PRIYANSHU KUMAR");

return 0;

}
Program for creating a array of n integer elements
using switch case

#include <stdio.h>

int main() {

int arr[100];

int n, choice, position, element;

printf("Enter the number of elements in the array: ");

scanf("%d", &n);

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

for (int i = 0; i < n; i++) {

printf("Element %d: ", i + 1);

scanf("%d", &arr[i]);

while (1) {

printf("\nArray Operations:\n");

printf("1. Display elements\n");

printf("2. Insert element at given position\n");

printf("3. Delete element at given position\n");

printf("4. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("\nElements in the array:\n");


for (int i = 0; i < n; i++) {

printf("%d ", arr[i]);

printf("\n");

break;

case 2:

printf("Enter the position to insert the element: ");

scanf("%d", &position);

if (position < 1 || position > n + 1) {

printf("Invalid position!\n");

} else {

printf("Enter the element to insert: ");

scanf("%d", &element);

for (int i = n - 1; i >= position - 1; i--) {

arr[i + 1] = arr[i];

arr[position - 1] = element;

n++;

printf("Element inserted successfully.\n");

break;

case 3:

printf("Enter the position to delete the element: ");

scanf("%d", &position);

if (position < 1 || position > n) {

printf("Invalid position!\n");

} else {

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

arr[i] = arr[i + 1];

n--;
printf("Element deleted successfully.\n");

break;

case 4:

printf("Exiting the program...\n");

return 0;

default:

printf("Invalid choice! Please enter a valid option.\n");

printf("\t\t\t PRIYANSHU KUMAR");

return 0;

Program for Merging the two array into a one array

#include <stdio.h>

int main() {

int arr1[100], arr2[100], mergedArray[200];

int n1, n2, mergedSize = 0;

printf("Enter the number of elements in the first array: ");

scanf("%d", &n1);

printf("Enter %d elements for the first array:\n", n1);

for (int i = 0; i < n1; i++) {

scanf("%d", &arr1[i]);
mergedArray[mergedSize++] = arr1[i];

printf("Enter the number of elements in the second array: ");

scanf("%d", &n2);

printf("Enter %d elements for the second array:\n", n2);

for (int i = 0; i < n2; i++) {

scanf("%d", &arr2[i]);

mergedArray[mergedSize++] = arr2[i];

printf("\nMerged Array:\n");

for (int i = 0; i < mergedSize; i++) {

printf("%d ", mergedArray[i]);

printf("\n");

printf("\t\t\t PRIYANSHU KUMAR");

return 0;

You might also like