Write a C program that uses functions to perform the following:
1. Addition of Two Matrices
#include<stdio.h>
void matrixAddition(int a[10][10], int b[10][10], int sum[10][10]);
int rows, columns;
void main()
{
int a[10][10], b[10][10], sum[10][10], i, j;
printf("Enter the no of rows and columns:");
scanf("%d%d", &rows, &columns);
/* input first matrix */
printf("Enter the input for first matrix:\n");
for (i = 0; i <rows; i++)
for (j = 0; j <columns; j++)
scanf("%d", &a[i][j]);
/* input second matrix */
printf("Enter the input for second matrix:\n");
for (i = 0; i < rows; i++)
for (j = 0; j< columns; j++)
scanf("%d", &b[i][j]);
/* matrix addtion */
matrixAddition(a, b, sum);
/* print the results */
printf("\nResult of Matrix Addition:\n");
for (i = 0; i < rows; i++)
{
for (j = 0; j < columns; j++)
printf("%d\t", sum[i][j]);
printf("\n";);
}
}
/* adds two matrices and stores the output in third matrix */
void matrixAddition(int a[10][10], int b[10][10], int sum[10][10])
{
int i, j;
for (i = 0; i< rows; i++)
for (j = 0; j < columns; j++)
sum[i][j] = a[i][j] + b[i][j];
}
2. Multiplication of Two Matrices
#include <stdio.h>
void mulmatrix(int [10][10],int [10][10],int,int,int,int);
void main()
{
int m, n, p, q, i,j, a[10][10], b[10][10];
printf("Enter number of rows and columns of first matrix");
scanf("%d%d", &m, & n);
printf("Enter elements of first matrix\n");
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
scanf("%d",&a[i][j]);
printf("Enter number of rows and columns of first matrix");
scanf("%d%d", &p, & q);
if (n != p)
printf("The matrices can't be multiplied with each other.\n");
else
{
printf("Enter elements of second matrix\n");
for (i = 0; i < p; i++)
for (j = 0; j < q; j++)
scanf("%d",&b[i][j]);
mulmatrix(a,b,m,n,p,q);
}
}
void mulmatrix(int a[10][10],int b[10][10],int m,int n,int p,int q)
{
int i,j,k,sum=0,mul[10][10];
for (i = 0; i < p; i++){
for (j = 0; j < q; j++){
for (k = 0; k < p; k++) {
sum = sum + a[i][k]*b[k][j];
}
mul[i][j] = sum;
sum = 0;
}
}
printf("Product of the matrices:\n");
for (i = 0; i < p; i++){
for (j = 0; j < q; j++)
printf("%d" ,mul[i][j]);
printf("\n");
}
}
g. Write C programs that use both recursive and non-recursive functions
1.To find the factorial of a given integer.
#include<stdio.h>
long int rfact(int );
void fact(int);
void main()
{
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
printf(" recursive function of Factorial of %d = %ld", n, rfact(n));
fact(n);
}
void fact(int x)
{
int i,f=1;
for(i=1’i<=x;i++)
f=f*I;
printf("Factorial of %d = %d", n, f);
}
long int rfact (int n)
{
if (n==1)
return 1;
else
return n*factorial(n-1);
}
2.To find the GCD (greatest common divisor) of two given integers.
#include <stdio.h>
int recgcd(int x, int y);
int nonrecgcd(int x, int y);
void main()
{
int a, b, c, d;
printf("Enter two numbers a and b");
scanf("%d%d", &a, &b);
c = recgcd(a, b);
printf("The gcd of two numbers using recursion is %d\n", c);
d = nonrecgcd(a, b);
printf("The gcd of two numbers using nonrecursion is %d", d);
}
int recgcd(int x, int y){
if(y == 0){
return(x);
}
else{
return(recgcd(y, x % y));
}
}
int nonrecgcd(int x, int y){
int z;
while(x % y != 0){
z = x % y;
x = y;
y = z;
}
return(y);
}
3.To find x^n
#include <stdio.h>
int rpower(int , int);
void power(int,int)
int main() {
int base, p, result;
printf("Enter base number:\n ");
scanf("%d", &base);
printf("Enter power number(positive integer):\n ");
scanf("%d", &p);
result = power(base, p);
printf("%d^%d = %d\n", base, a, result);
power(base,p);
return 0;
}
void power(int base,int p)
{
int i,re=1;
for(i=1;i<=p;i++)
re=re*base;
printf("%d^%d = %d\n", base, a, re);
}
int power(int base, int p) {
if (p != 0)
return (base * power(base, p - 1));
else
return 1;
}