ASSIGNMENT 8
1. Write a program in C to perform the following functions on two 2D arrays entered by user:
a) Addition of matrices
b) Multiplication of matrices
c) Transpose of matrix
#include <stdio.h>
// Function to add two matrices
void addMatrices(int rows, int cols, int mat1[rows][cols], int mat2[rows][cols], int result[rows][cols]) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[i][j] = mat1[i][j] + mat2[i][j];
// Function to multiply two matrices
void multiplyMatrices(int rows1, int cols1, int mat1[rows1][cols1], int rows2, int cols2, int
mat2[rows2][cols2], int result[rows1][cols2]) {
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
result[i][j] = 0;
for (int k = 0; k < cols1; k++) {
result[i][j] += mat1[i][k] * mat2[k][j];
// Function to transpose a matrix
void transposeMatrix(int rows, int cols, int mat[rows][cols], int result[cols][rows]) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[j][i] = mat[i][j];
// Function to display a matrix
void displayMatrix(int rows, int cols, int mat[rows][cols]) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", mat[i][j]);
printf("\n");
int main() {
int rows1, cols1, rows2, cols2;
// Input dimensions for the first matrix
printf("Enter the number of rows and columns for the first matrix: ");
scanf("%d %d", &rows1, &cols1);
// Input dimensions for the second matrix
printf("Enter the number of rows and columns for the second matrix: ");
scanf("%d %d", &rows2, &cols2);
// Ensure matrices can be multiplied
if (cols1 != rows2) {
printf("Matrices cannot be multiplied due to incompatible dimensions.\n");
return 1;
}
int mat1[rows1][cols1], mat2[rows2][cols2], resultAdd[rows1][cols1], resultMul[rows1][cols2],
resultTrans[cols1][rows1];
// Input elements for the first matrix
printf("Enter elements of the first matrix:\n");
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols1; j++) {
scanf("%d", &mat1[i][j]);
// Input elements for the second matrix
printf("Enter elements of the second matrix:\n");
for (int i = 0; i < rows2; i++) {
for (int j = 0; j < cols2; j++) {
scanf("%d", &mat2[i][j]);
// Perform addition
addMatrices(rows1, cols1, mat1, mat2, resultAdd);
printf("Result of matrix addition:\n");
displayMatrix(rows1, cols1, resultAdd);
// Perform multiplication
multiplyMatrices(rows1, cols1, mat1, rows2, cols2, mat2, resultMul);
printf("Result of matrix multiplication:\n");
displayMatrix(rows1, cols2, resultMul);
// Perform transpose of the first matrix
transposeMatrix(rows1, cols1, mat1, resultTrans);
printf("Transpose of the first matrix:\n");
displayMatrix(cols1, rows1, resultTrans);
return 0;
2. Write a program in C to take a number as input and find the factorial of this number using
functions.
#include <stdio.h>
int factorial(int num){
int prod = 1;
for(int i = 1;i<=num; i++){
prod = prod*i;
return prod;
int main(){
int num ;
printf("Enter the number\n");
scanf("%d", &num);
int fact = factorial(num);
printf("The factorial is %d", fact);
return 0;
3. Write a program in C to take a number as input and find the if the number is prime or not using
functions.
int isPrime(int number) {
if (number <= 1) return 0; // 0 and 1 are not prime numbers
for (int i = 2; i <= number / 2; i++) {
if (number % i == 0) return 0; // Not a prime number
return 1; // Prime number
int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
if (isPrime(number))
printf("%d is a prime number.\n", number);
else
printf("%d is not a prime number.\n", number);
return 0;