1. P a g e | 1
NB : Make necessary corrections if required
1. Find factorial of a number
#include<stdio.h>
int main()
{
int i,n;
unsigned long long int fact=1;
printf("Enter the number whose factorial is to be found:n");
scanf("%d",&n);
if(n<0)
printf("Error!, The factorial of a negative number does not exist:n");
else
{
if(n==0)
fact=1;
else
for(i=1;i<=n;i++)
fact=fact*i;
printf("Factorial of the given number is:%d",fact);
}
}
2. Obtain Fibonacci series
#include<stdio.h>
int main()
{
int n,i,F1=1,F2=1,F;
printf("Enter the number of elements in series:");
scanf("%d",&n);
//show error if the user enters a negative integer
if(n<0)
printf("Error! number of elements cannot be negative");
else
printf("The Fibonacci series is");
printf("%d%d",F1,F2);
{for(i=3;i<=n;++i)
{
F=F1+F2;
F1=F2;
F2=F;
printf("%d",F);
}
}
return 0;
}
2. P a g e | 2
NB : Make necessary corrections if required
3. To check for a prime number
#include<stdio.h>
#include<math.h>
int main()
{
int n,f=0,d=2;
printf("Enter the number to check whether prime or not:n");
scanf("%d",&n);
do
{
if(n%d==0)
f=1;
d++;
}while(d<=n/2 && f==0);
if(n==2)
f=0;
if(f==0)
printf("The given number is prime:n");
else
printf("The given number is not prime:n");
return 0;
}
4. To write and run C programs to sort a set of numbers in ascending & descending order
using bubble sorting algorithm
#include <stdio.h>
void readarray(int a[],int m)
{
int i;
printf("Enter the elements:n");
for(i=0;i<m;i++) scanf("%d",&a[i]);
}
void swap(int *a,int *b)
{
int temp=*a;
*a=*b;
*b=temp;
}
void ascending(int a[],int m)
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<m-1;j++)
3. P a g e | 3
NB : Make necessary corrections if required
if(a[j]>a[j+1])
swap(&a[j],&a[j+1]);
}
for(i=0;i<m;i++)
printf("%dt",a[i]);
}
void descending(int a[],int m)
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<m-1;j++)
if(a[j]<a[j+1])
swap(&a[j],&a[j+1]);
}
for(i=0;i<m;i++)
printf("%dt",a[i]);
}
int main()
{
int n,i,asd[100];
printf("Enter the number of elements:n");
scanf("%d",&n);
readarray(asd,n);
printf("Sorted array in ascending order is:n");
ascending(asd,n);
printf("nSorted array in descending order is:n");
descending(asd,n);
}
5. Write C programs to find inverse of a matrix
#include<stdio.h>
void Swaprows(float a[10][10],int r1,int r2,int n)
{
int j;
float temp;
for(j=0;j<n;j++)
{
temp=a[r1][j];
a[r1][j]=a[r2][j];
a[r2][j]=temp;
}
}
float determinant(float matrix[10][10],int n)
{
int i;
float det=1;
for(i=0;i<n;i++)
4. P a g e | 4
NB : Make necessary corrections if required
det=det*matrix[i][i];
printf("nnDeterminant of the Given matrix is %fn",det);
if(det==0)
{
printf("nnInverse can't be find when determinant is zero:n");
}
return det;
}
void uppertriangular(float matrix[10][10],float idnt[10][10],int n)
{
int i,j,k;
float ratio;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i>j)
{
ratio = matrix[i][j]/matrix[j][j];
for(k=0;k<n;k++)
{
matrix[i][k]=matrix[i][k]-ratio*matrix[j][k];
idnt[i][k]=idnt[i][k]-ratio*idnt[j][k];
}
}
}
}
}
void lowertriangular(float matrix[10][10],float idnt[10][10],int n)
{
int i,j,k;
float ratio;
for(i=n-1;i>=0;i--)
{
for(j=n-1;j>=0;j--)
{
if(i<j)
{
ratio = matrix[i][j]/matrix[j][j];
for(k=0;k<n;k++)
{
matrix[i][k]=matrix[i][k]-ratio*matrix[j][k];
idnt[i][k]=idnt[i][k]-ratio*idnt[j][k];
}
}
}
}
}
void RowDiv(float a[10][10],float b[10][10],int r,int n,float T)
5. P a g e | 5
NB : Make necessary corrections if required
{
int j;
for(j=0;j<n;j++)
{
a[r][j]=a[r][j]/T;
b[r][j]=b[r][j]/T;
}
}
void print_matrix(float matrix[10][10],int n)
{
int i,j;
for(i=0;i<n;i++)
{
printf("n");
for(j=0;j<n;j++)
{
printf("%ft",matrix[i][j]);
}
}
}
int main()
{
float matrix[10][10],ratio,idnt[10][10];
int i,j,k,n;
printf("Enter the order of matrix:n");
scanf("%d",&n);
printf("Enter the matrix:n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
scanf("%f",&matrix[i][j]);
}
printf("nthe matrix is n");
print_matrix(matrix,n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
if(i==j)
idnt[i][j]=1;
else
idnt[i][j]=0;
}
printf("nIdentity matrix is n");
print_matrix(idnt,n);
for(i=0;i<n;i++)
{
if(matrix[i][i]==0)
{
if(i=n-1)
6. P a g e | 6
NB : Make necessary corrections if required
Swaprows(matrix,i,0,n);
else
Swaprows(matrix,i,i+1,n);
}
}
printf("nnAfter Swaping Operation The given Matrix is changed to: n");
print_matrix(matrix,n);
lowertriangular(matrix,idnt,n);
if(determinant(matrix,n)!=0)
{
uppertriangular(matrix,idnt,n);
printf("nThe given Matrix is changed to: n");
print_matrix(matrix,n);
printf("nIdentity matrix is changed to:n");
print_matrix(idnt,n);
for(i=0;i<n;i++)
RowDiv(matrix,idnt,i,n,matrix[i][i]);
printf("nThe given Matrix is changed to: n");
print_matrix(matrix,n);
printf("nIdentity matrix is changed to:n");
print_matrix(idnt,n);
printf("nInverse of the Given matrix is:n");
print_matrix(idnt,n);
}
}
6. Write C programs to find sum of two matrices
#include <stdio.h>
void read_matrices(int mat1[10][10],int mat2[10][10],int r1,int r2,int c1,int c2)
{
int i,j;
printf("Enter the elements of 1st matrix:n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
scanf("%d",&mat1[i][j]);
}
printf("Enter the elements of 2nd matrix:n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
scanf("%d",&mat2[i][j]);
}
}
void Addition(int mat1[10][10],int mat2[10][10],int mat3[10][10],int r1,int r2,int c1,int
c2)
{
int i,j;
7. P a g e | 7
NB : Make necessary corrections if required
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
mat3[i][j]=mat1[i][j]+mat2[i][j];
}
}
void print_matrices(int mat1[10][10],int mat2[10][10],int mat3[10][10],int r1,int r2,int
c1,int c2)
{
int i,j;
printf("nThe 1st matrix is:n");
for(i=0;i<r1;i++)
{
printf("n");
for(j=0;j<c1;j++)
printf("%d t",mat1[i][j]);
}
printf("nThe 2nd matrix is:n");
for(i=0;i<r2;i++)
{
printf("n");
for(j=0;j<c2;j++)
printf("%d t",mat2[i][j]);
}
printf("nThe Addition Result is:n");
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
printf("%d t",mat3[i][j]);
printf("n");
}
}
int main()
{
int r1,r2,c1,c2,mat1[10][10],mat2[10][10],mat3[10][10];
printf("Enter the no.of rows & columns of Matrix 1 n");
scanf("%d %d",&r1,&c1);
printf("Enter the no.of rows & columns of Matrix 2 n");
scanf("%d %d",&r2,&c2);
if(r1==r2 && c1==c2)
{
read_matrices(mat1,mat2,r1,r2,c1,c2);
Addition(mat1,mat2,mat3,r1,r2,c1,c2);
print_matrices(mat1,mat2,mat3,r1,r2,c1,c2);
}
else
printf("Error! nAddition is not possible if both matrices are of same order n");
return 0;
}
8. P a g e | 8
NB : Make necessary corrections if required
7. Write C programs to find product of two matrices
#include <stdio.h>
void read_matrices(int mat1[10][10],int mat2[10][10],int r1,int r2,int c1,int c2)
{
int i,j;
printf("Enter the elements of 1st matrix:n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
scanf("%d",&mat1[i][j]);
}
printf("Enter the elements of 2nd matrix:n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
scanf("%d",&mat2[i][j]);
}
}
void multiplication(int mat1[10][10],int mat2[10][10],int mat3[10][10],int r1,int r2,int
c1,int c2)
{
int i,j,k,sum;
for(i=0;i<r1;i++)
for(j=0;j<c2;j++)
mat3[i][j]=0;
for(i=0;i<r1;i++)
for(j=0;j<c2;j++)
for(k=0;k<c1;k++)
mat3[i][j]=mat3[i][j]+mat1[i][k]*mat2[k][j];
}
void print_matrices(int mat1[10][10],int mat2[10][10],int mat3[10][10],int r1,int r2,int
c1,int c2)
{
int i,j;
printf("nThe 1st matrix is:n");
for(i=0;i<r1;i++)
{
printf("n");
for(j=0;j<c1;j++)
printf("%d t",mat1[i][j]);
}
printf("nThe 2nd matrix is:n");
for(i=0;i<r2;i++)
{
printf("n");
for(j=0;j<c2;j++)
printf("%d t",mat2[i][j]);
9. P a g e | 9
NB : Make necessary corrections if required
}
printf("nThe multiplication Result is:n");
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
printf("%d t",mat3[i][j]);
printf("n");
}
}
int main()
{
int r1,r2,c1,c2,mat1[10][10],mat2[10][10],mat3[10][10];
printf("Enter the no.of rows & columns of Matrix 1 n");
scanf("%d %d",&r1,&c1);
printf("Enter the no.of rows & columns of Matrix 2 n");
scanf("%d %d",&r2,&c2);
if(r2==c1)
{
read_matrices(mat1,mat2,r1,r2,c1,c2);
multiplication(mat1,mat2,mat3,r1,r2,c1,c2);
print_matrices(mat1,mat2,mat3,r1,r2,c1,c2);
}
else
printf("Error! nMultiplication is not possiblen");
return 0;
}
8. Write C programs to find determinant of a matrix
#include<stdio.h>
int main()
{
float matrix[10][10],ratio,det=1;
int i,j,k,n;
printf("Enter the order of matrix:");
scanf("%d",&n);
printf("Enter the matrix: n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
scanf("%f",&matrix[i][j]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i>j)
{
ratio = matrix[i][j]/matrix[j][j];
10. P a g e | 10
NB : Make necessary corrections if required
for(k=0;k<n;k++)
{
matrix[i][k]=matrix[i][k]-ratio*matrix[j][k];
}
}
}
}
printf("the upper triangular matrix is ");
for(i=0;i<n;i++)
{
printf("n");
for(j=0;j<n;j++)
{
printf("%ft",matrix[i][j]);
}
}
for(i=0;i<n;i++)
det=det*matrix[i][i];
printf("nDeterminand of given matrix is:%f",det);
}
9. Using C program find
( )!
, where m and n are integers.
#include <stdio.h>
int main()
{
int n,fact=1,i,m,r;
printf("Enter the number 'n':n");
scanf("%d",&n);
printf("Enter the number 'm':n");
scanf("%d",&m);
for(i=1;i<n;i++)
fact=fact*i;
r=fact/m;
printf("The result is:%d",r);
}
10. Write a c program to obtain the series 7 7 14 21 35 up to n terms
#include <stdio.h>
int main()
{
int f1=7,f2=7,f,n;
printf("Enter the number of elements in the fibonacci series:t");
scanf("%d",&n);
if(n<=0)
printf("Error! Number of elements can not be zero or negative:n");
else
11. P a g e | 11
NB : Make necessary corrections if required
{
printf("nFibonacci series is:");
printf("n%dt%d",f1,f2);
for(int i=3;i<=n;i++)
{
f=f1+f2;
printf("t%d",f);
f1=f2;
f2=f;
}
}
}
11. Write a C program to list all prime numbers between 10 and 20
#include<stdio.h>
int main()
{
int n,f,d;
for(n=10;n<=20;n++)
{
f=0;
for(d=2;d<n/2;d++)
{
if(n%d==0)
f=1;
}
if(f==0)
printf("%dn",n);
}
}
12. A number is given. Check whether it is prime or not ? If so, get next even number. Else,
get next odd number.
#include<stdio.h>
int main()
{
int n,f=0,d=2;
printf("Enter the number : n");
scanf("%d",&n);
for(d=2;d<n/2;d++)
{
if(n%d==0)
f=1;
}
if(f==0)
{
printf("The Number %d is primen",n);
12. P a g e | 12
NB : Make necessary corrections if required
if(n%2==0)
printf("The next even number is %d n",n+2);
else
printf("The next even number is %d n",n+1);
}
if(f==1)
{
printf("The Number %d is not primen",n);
if(n%2==1)
printf("The next odd number is %d n",n+2);
else
printf("The next odd number is %d n",n+1);
}
}
13. Write a program to interchange second and fourth numbers of an array
#include <stdio.h>
int main()
{
int array[10],i,n,temp;
printf("Enter size of arrayn");
scanf("%d",&n);
printf("Enter elements of arrayn");
for(i=0;i<n;i++)
scanf("%d",&array[i]);
printf("The elements of given array aren");
for(i=0;i<n;i++)
printf("array[%d]=%dn",i,array[i]);
temp=array[1];
array[1]=array[3];
array[3]=temp;
printf("The elements of array after swaping aren");
for(i=0;i<n;i++)
printf("array[%d]=%dn",i,array[i]);
}
14. Using C program to find largest number from an array
#include<stdio.h>
int main()
{
int n,i,a[100],lr;
printf("Enter the number of elements:n ");
scanf("%d",&n);
printf("Enter the number of elements:n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
lr=a[0];
13. P a g e | 13
NB : Make necessary corrections if required
for(i=0;i<n-1;i++)
{
if(a[i+1]>lr)
lr=a[i+1];
}
printf("nLargest number is %d",lr);
return 0;
}
15. Write C program to interchange first and third rows of a matrix
#include <stdio.h>
int main()
{
int n,m[10][10],i,j,temp;
printf("Enter the order of matrix:n");
scanf("%d",&n);
printf("enter the elementsn");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
scanf("%d",&m[i][j]);
}
printf("given matrix isn");
for(i=0;i<n;i++)
{
printf("n");
for(j=0;j<n;j++)
printf("%dt",m[i][j]);
}
for(j=0;j<n;j++)
{
temp=m[0][j];
m[0][j]=m[2][j];
m[2][j]=temp;
}
printf("nthe result isn");
for(i=0;i<n;i++)
{
printf("n");
for(j=0;j<n;j++)
printf("%dt",m[i][j]);
}
}
16. Write C program to interchange a row and next row of a matrix
#include <stdio.h>
int main()
14. P a g e | 14
NB : Make necessary corrections if required
{
int n,m[10][10],i,j,temp,k;
printf("Enter the order of matrix:n");
scanf("%d",&n);
printf("enter the elementsn");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
scanf("%d",&m[i][j]);
}
printf("given matrix isn");
for(i=0;i<n;i++)
{
printf("n");
for(j=0;j<n;j++)
printf("%dt",m[i][j]);
}
printf("nEnter the row number you want to interchange:n");
scanf("%d",&k);
for(j=0;j<n;j++)
{
temp=m[k-1][j];
m[k-1][j]=m[k][j];
m[k][j]=temp;
}
printf("nthe result isn");
for(i=0;i<n;i++)
{
printf("n");
for(j=0;j<n;j++)
printf("%dt",m[i][j]);
}
}
17. An nxn matrix is given. Convert it into an upper triangular matrix using C
program
#include<stdio.h>
int main()
{
float matrix[10][10],ratio;
int i,j,k,n;
printf("Enter the order of matrix:n");
scanf("%d",&n);
printf("Enter the elements of matrix: n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
scanf("%f",&matrix[i][j]);
15. P a g e | 15
NB : Make necessary corrections if required
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i>j)
{
ratio = matrix[i][j]/matrix[j][j];
for(k=0;k<n;k++)
{
matrix[i][k]=matrix[i][k]-ratio*matrix[j][k];
}
}
}
}
printf("the upper triangular matrix is ");
for(i=0;i<n;i++)
{
printf("n");
for(j=0;j<n;j++)
{
printf("%ft",matrix[i][j]);
}
}
}
18. An nxn matrix is given. Convert it into a lower triangular matrix using C program
#include<stdio.h>
int main()
{
float matrix[10][10],ratio;
int i,j,k,n;
printf("Enter the order of matrix:");
scanf("%d",&n);
printf("Enter the matrix: n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
scanf("%f",&matrix[i][j]);
}
for(i=n-1;i>=0;i--)
{
for(j=n-1;j>=0;j--)
{
if(i<j)
{
ratio = matrix[i][j]/matrix[j][j];
16. P a g e | 16
NB : Make necessary corrections if required
for(k=0;k<n;k++)
{
matrix[i][k]=matrix[i][k]-ratio*matrix[j][k];
}
}
}
}
printf("the lower triangular matrix is ");
for(i=0;i<n;i++)
{
printf("n");
for(j=0;j<n;j++)
{
printf("%ft",matrix[i][j]);
}
}
}
19. Consider the augmented matrix Ab = A/I, where A is an nXn matrix and I is an identity
matrix of order n. Do same elementary operations on both A and I so that A is
transformed into an upper triangular matrix. Write a C program.
#include<stdio.h>
void print_matrix(float matrix[10][10],int n)
{
int i,j;
for(i=0;i<n;i++)
{
printf("n");
for(j=0;j<n;j++)
{
printf("%ft",matrix[i][j]);
}
}
}
int main()
{
float matrix[10][10],ratio,idnt[10][10];
int i,j,k,n;
printf("Enter the order of matrix:");
scanf("%d",&n);
printf("Enter the matrix: n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
scanf("%f",&matrix[i][j]);
}
for(i=0;i<n;i++)
17. P a g e | 17
NB : Make necessary corrections if required
{
for(j=0;j<n;j++)
if(i==j)
idnt[i][j]=1;
else
idnt[i][j]=0;
}
printf("nIdentity matrix is n");
print_matrix(idnt,n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i>j)
{
ratio = matrix[i][j]/matrix[j][j];
for(k=0;k<n;k++)
{
matrix[i][k]=matrix[i][k]-ratio*matrix[j][k];
idnt[i][k]=idnt[i][k]-ratio*idnt[j][k];
}
}
}
}
printf("nthe upper triangular matrix is n");
print_matrix(matrix,n);
printf("nIdentity matrix is changed ton");
print_matrix(idnt,n);
}
20. Using C program, find + , where A and B are matrices.
#include <stdio.h>
int main()
{
int i,j,n,a[10][10],b[10][10],c[10][10],d[10][10],k;
printf("Enter the order of matrix :n");
scanf("%d",&n);
printf("Enter the elements of matrix A:n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
printf("Enter the elements of matrix B:n");
for(i=0;i<n;i++)
{
18. P a g e | 18
NB : Make necessary corrections if required
for(j=0;j<n;j++)
{
scanf("%d",&b[i][j]);
c[i][j]=0;
}
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
for(k=0;k<n;k++)
c[i][j]=c[i][j]+a[i][k]*a[k][j];
}
printf("nThe A^2 matrix is:n");
for(i=0;i<n;i++)
{
printf("n");
for(j=0;j<n;j++)
printf("%d t",c[i][j]);
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
d[i][j]=c[i][j]+b[i][j];
printf("nThe Final result is:n");
for(i=0;i<n;i++)
{
printf("n");
for(j=0;j<n;j++)
printf("%d t",d[i][j]);
}
}
21. Write a C program to find the solution of
= sin
Using Euler method.
#include<stdio.h>
#include<math.h>
float function(float x,float t)
{
float f=sin(x);
return f;
}
void euler(int n,float t0,float tn,float x0,float x[100],float t[100])
{
int k=0;
x[0]=x0;
t[0]=t0;
19. P a g e | 19
NB : Make necessary corrections if required
float h=(tn-t0)/n;
while(t[k]!=tn)
{
x[k+1]=x[k]+h*function(x[k],t[k]);
t[k+1]=t[k]+h;
k++;
}
}
int main()
{
int i,n;
float t0,tn,x0,t[100],x[100];
printf("nEnter the initial & final times nEnter no.of iterations:n");
scanf("%f%f%d",&t0,&tn,&n);
printf("Enter the initial value:x0n");
scanf("%f",&x0);
printf("nsolution using euler's methodnn");
euler(n,t0,tn,x0,x,t);
printf("ttxnn");
for(i=0;i!=n;i++)
printf("%2.4ft%2.4fn",t[i],x[i]);
return 0;
}
22. Write a C program to find the solution of
= cos
using Ranga-Kutta method.
#include<stdio.h>
#include<math.h>
float fnctn(float x,float t)
{
float f;
f=cos(x);
return f;
}
void RK(int n,float t0,float tn,float x0,float x[100],float t[100])
{
float h,K1,K2,K3,K4;
int m;
t[0]=t0;
x[0]=x0;
m=0;
h=(tn-t0)/n;
while(t[m]<=tn)
{
K1=h*fnctn(x[m],t[m]);
20. P a g e | 20
NB : Make necessary corrections if required
K2=h*fnctn(x[m]+K1/2,t[m]+h/2);
K3=h*fnctn(x[m]+K2/2,t[m]+h/2);
K4=h*fnctn(x[m]+K3/2,t[m]+h);
x[m+1]=x[m]+(K1+2*K2+2*K3+K4)/6;
t[m+1]=t[m]+h;
m=m+1;
}
}
int main()
{
int n,i;
float x[100],t[100],t0,tn;
printf("enter initial and final times and number of iterations:n");
scanf("%f%f%d",&t0,&tn,&n);
printf("enter the initial value x0:");
scanf("%f",&x[0]);
printf("n solution using Runge-Kutta methodnn");
RK(n,t0,tn,x[0],x,t);
printf("ttxn");
for(i=0;i!=n;i++)
printf("%ft%fn",t[i],x[i]);
return 0;
}
23. Using C program,find the solution of linear equations. (Use Gauss elimination method)
#include<stdio.h>
int main()
{
int i,j,k,n;
float A[20][20],c,x[10],sum=0.0;
printf("nEnter the order of matrix: ");
scanf("%d",&n);
printf("nEnter the elements of augmented matrix row-wise:nn");
for(i=1; i<=n; i++)
{
for(j=1; j<=(n+1); j++)
{
printf("A[%d][%d] : ", i,j);
scanf("%f",&A[i][j]);
}
}
for(j=1; j<=n; j++) /* loop for the generation of upper triangular matrix*/
{
for(i=1; i<=n; i++)
{
if(i>j)
{
c=A[i][j]/A[j][j];
21. P a g e | 21
NB : Make necessary corrections if required
for(k=1; k<=n+1; k++)
{
A[i][k]=A[i][k]-c*A[j][k];
}
}
}
}
x[n]=A[n][n+1]/A[n][n];
/* this loop is for backward substitution*/
for(i=n-1; i>=1; i--)
{
sum=0;
for(j=i+1; j<=n; j++)
{
sum=sum+A[i][j]*x[j];
}
x[i]=(A[i][n+1]-sum)/A[i][i];
}
printf("nThe solution is: n");
for(i=1; i<=n; i++)
{
printf("nx%d=%ft",i,x[i]); /* x1, x2, x3 are the required solutions*/
}
return(0);
}
24. Using C program,find the solution of linear equations. (Use Gauss siedal method)
#include<stdio.h>
int main()
{
float a[10][10],b[10],x[10],y[10];
intn,m,i,j;
printf("Enter the no. of linear equation n");
scanf("%d",&n);
printf("Enter the coefficients of the equationsn");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("Matrix[%d][%d] = ",i,j);
scanf("%f",&a[i][j]);
}
}
printf("nEnter Values of the right side of equationn");
for(i=0;i<n;i++)
{
printf("b[%d] = ",i);
22. P a g e | 22
NB : Make necessary corrections if required
scanf("%f",&b[i]);
}
printf("Enter initial values of xn");
for(i=0;i<n;i++)
{
printf("x[%d] = ",i);
scanf("%f",&x[i]);
}
printf("nEnter the no. of iteration :");
scanf("%d",&m);
while(m>0)
{
for(i=0;i<n;i++)
{
y[i]=(b[i]/a[i][i]);
for(j=0;j<n;j++)
{
if(j==i)
continue;
y[i]=y[i]-((a[i][j]/a[i][i])*x[j]);
x[i]=y[i];
}
printf("x%d=%6.2f ",i+1,y[i]);
}
printf("n");
m--;
}
return 0;
}
25. Using C program, find the sum of first n Fibonacci numbers
#include<stdio.h>
int main()
{
int n,i,F1=1,F2=1,F,sum=F1+F2;
printf("Enter the number of elements in series:t");
scanf("%d",&n);
if(n<0)
printf("Error! number of elements cannot be negative");
else
{
printf("The Fibonacci series isn");
printf("%dt%dt",F1,F2);
{
for(i=3;i<=n;++i)
{
F=F1+F2;
sum=sum+F;
23. P a g e | 23
NB : Make necessary corrections if required
F1=F2;
F2=F;
printf("%dt",F);
}
printf("nThe sum of the fibonacci series is:t%d",sum);
}
}
return 0;
}
26. Use C program, to get number of Fibonacci numbers between 1 and n, where n is an
integer.
#include<stdio.h>
int main()
{
int n,i=3,F1=1,F2=1,F,sum=F1+F2,j=2;;
printf("Enter the number n :t");
scanf("%d",&n);
if(n<0)
printf("Error! number of elements cannot be negative");
else
{
printf("The Fibonacci series isn");
printf("%dt%dt",F1,F2);
while((F1+F2)<=n)
{
F=F1+F2;
F1=F2;
F2=F;
printf("%dt",F);
i++;
j++;
}
printf("nThe no.of elements in fibonacci series is : t%d",j);
}
return 0;
}
27. Use C program, to get number of odd Fibonacci numbers between 1 and n, where n is
an integer. Using C program list all even Fibonacci numbers between 1 and n, where n
is an integer.
#include<stdio.h>
int main()
{
int n,F1=1,F2=1,F,sum=F1+F2,j=2;;
printf("Enter the number n :t");
24. P a g e | 24
NB : Make necessary corrections if required
scanf("%d",&n);
if(n<0)
printf("Error! number of elements cannot be negative");
else
{
printf("The Even Fibonacci series isn");
while((F1+F2)<=n)
{
F=F1+F2;
F1=F2;
F2=F;
if(F%2==0)
printf("%dt",F);
else
j++;
}
printf("nThe no.of odd elements in fibonacci series is : t%d",j);
}
return 0;
}
28. With a C program, get
!
( − )! !
Use minimum number of codes (use functions).
#include<stdio.h>
long long int factorial(int m)
{
long long int fact=1;
for(int i=1;i<=m;i++)
fact=fact*i;
return fact;
}
int main()
{
int n,r;
long long int k;
printf("Enter the numbers n & r :n");
scanf("%d%d",&n,&r);
k=factorial(n)/(factorial(n-r)*factorial(r));
printf("The %dC%d is :t%d",n,r,k);
}
29. Use C program to list all prime numbers between 1 and n.
#include<stdio.h>
25. P a g e | 25
NB : Make necessary corrections if required
int main()
{
int n,m,f,d;
printf("Enter the upper limit n:n");
scanf("%d",&m);
printf("Prime numbers between 1 & %d isn",m);
for(n=1;n<=m;n++)
{
f=0;
for(d=2;d<=n/2;d++)
{
if(n%d==0)
f=1;
}
if(f==0)
printf("%dn",n);
}
}
30. A set of numbers are given. With a c program, group them to prime and composite
numbers and then arrange each group in descending order.
#include<stdio.h>
void swap(int *a,int *b)
{
int temp=*a;
*a=*b;
*b=temp;
}
void sorting(int a[50],int m)
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<m-1;j++)
if(a[j]<a[j+1])
swap(&a[j],&a[j+1]);
}
}
void print_numbers(int prime[50],int not_prime[50],int k,int j)
{
int i;
printf("The list of prime numbers is:n");
for(i=0;i<j;i++)
printf("%dn",prime[i]);
printf("The list of composite numbers is:n");
for(i=0;i<k;i++)
printf("%dn",not_prime[i]);
}
void prime_numbers(int a[50],int n)
26. P a g e | 26
NB : Make necessary corrections if required
{
int prime[50],not_prime[50];
int f,d,i,j=0,k=0;
for(i=0;i<n;i++)
{
f=0;
for(d=2;d<=n/2;d++)
{
if(a[i]%d==0)
f=1;
}
if(f==0)
{
prime[j]=a[i];
j++;
}
else
{
not_prime[k]=a[i];
k++;
}
}
sorting(prime,j);
sorting(not_prime,k);
print_numbers(prime,not_prime,k,j);
}
int main()
{
int n,f,d,i,a[50],j=0,k=0,prime[50],not_prime[50];
printf("Enter the no.of elements n:n");
scanf("%d",&n);
printf("Enter the elements:n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
prime_numbers(a,n);
}
31. A list of integers are given. Arrange the numbers such that first three numbers are
arranged in descending order and remaining in ascending order. Use minimum number
of codes
#include<stdio.h>
void readarray(int a[],int m)
{
int i;
printf("Enter the elements:n");
for(i=0;i<m;i++)
scanf("%d",&a[i]);
}
27. P a g e | 27
NB : Make necessary corrections if required
void swap(int *a,int *b)
{
int temp=*a;
*a=*b;
*b=temp;
}
void ascending(int a[],int m)
{
int i,j;
for(i=3;i<m;i++)
{
for(j=3;j<m-1;j++)
if(a[j]>a[j+1])
swap(&a[j],&a[j+1]);
}
}
void descending(int a[],int m)
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<m-1;j++)
if(a[j]<a[j+1])
swap(&a[j],&a[j+1]);
}
}
int main()
{
int n,i,asd[100];
printf("Enter the number of elements:n");
scanf("%d",&n);
readarray(asd,n);
printf("Sorted array in which 1st 3 numbers are in descending order & remaining
in ascending order is:n");
descending(asd,3);
ascending(asd,n);
for(i=0;i<n;i++)
printf("%dt",asd[i]);
}
32. Show that ∗ =
#include<stdio.h>
void Swaprows(float a[10][10],int r1,int r2,int n)
{
int j;
float temp;
for(j=0;j<n;j++)
{
28. P a g e | 28
NB : Make necessary corrections if required
temp=a[r1][j];
a[r1][j]=a[r2][j];
a[r2][j]=temp;
}
}
float determinant(float matrix[10][10],int n)
{
int i;
float det=1;
for(i=0;i<n;i++)
det=det*matrix[i][i];
printf("nnDeterminant of the Given matrix is %fn",det);
if(det==0)
{
printf("nnInverse can't be find when determinant is zero:n");
}
return det;
}
void uppertriangular(float matrix[10][10],float idnt[10][10],int n)
{
int i,j,k;
float ratio;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i>j)
{
ratio = matrix[i][j]/matrix[j][j];
for(k=0;k<n;k++)
{
matrix[i][k]=matrix[i][k]-ratio*matrix[j][k];
idnt[i][k]=idnt[i][k]-ratio*idnt[j][k];
}
}
}
}
}
void lowertriangular(float matrix[10][10],float idnt[10][10],int n)
{
int i,j,k;
float ratio;
for(i=n-1;i>=0;i--)
{
for(j=n-1;j>=0;j--)
{
if(i<j)
{
ratio = matrix[i][j]/matrix[j][j];
29. P a g e | 29
NB : Make necessary corrections if required
for(k=0;k<n;k++)
{
matrix[i][k]=matrix[i][k]-ratio*matrix[j][k];
idnt[i][k]=idnt[i][k]-ratio*idnt[j][k];
}
}
}
}
}
void RowDiv(float a[10][10],float b[10][10],int r,int n,float T)
{
int j;
for(j=0;j<n;j++)
{
a[r][j]=a[r][j]/T;
b[r][j]=b[r][j]/T;
}
}
void print_matrix(float matrix[10][10],int n)
{
int i,j;
for(i=0;i<n;i++)
{
printf("n");
for(j=0;j<n;j++)
printf("%ft",matrix[i][j]);
}
}
void multiplication(float matrix1[10][10],float matrix2[10][10],int n)
{
int i,j,k;
float matrix3[10][10],sum;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
sum=0;
for(k=0;k<n;k++)
sum=sum+matrix1[i][k]*matrix2[k][j];
matrix3[i][j]=sum;
}
print_matrix(matrix3,n);
}
int main()
{
float matrix[10][10],ratio,inverse[10][10],idnt[10][10],matrix2[10][10];
int i,j,k,n;
printf("Enter the order of matrix:n");
scanf("%d",&n);
printf("Enter the matrix:n");
30. P a g e | 30
NB : Make necessary corrections if required
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
scanf("%f",&matrix[i][j]);
}
printf("nthe matrix is n");
print_matrix(matrix,n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
if(i==j)
inverse[i][j]=1;
else
inverse[i][j]=0;
}
printf("nIdentity matrix is n");
print_matrix(inverse,n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
matrix2[i][j]=matrix[i][j];
for(i=0;i<n;i++)
{
if(matrix[i][i]==0)
{
if(i=n-1)
Swaprows(matrix,i,0,n);
else
Swaprows(matrix,i,i+1,n);
}
}
printf("nnAfter Swaping Operation The given Matrix is changed to: n");
print_matrix(matrix,n);
lowertriangular(matrix,inverse,n);
if(determinant(matrix,n)!=0)
{
uppertriangular(matrix,inverse,n);
printf("nThe given Matrix is changed to: n");
print_matrix(matrix,n);
printf("nIdentity matrix is changed to:n");
print_matrix(inverse,n);
for(i=0;i<n;i++)
RowDiv(matrix,inverse,i,n,matrix[i][i]);
printf("nThe given Matrix is changed to: n");
print_matrix(matrix,n);
printf("nIdentity matrix is changed to:n");
print_matrix(inverse,n);
printf("nInverse of the Given matrix is:n");
print_matrix(inverse,n);
printf("nThe Given matrix is : n");
31. P a g e | 31
NB : Make necessary corrections if required
print_matrix(matrix2,n);
printf("nThe multiplication result is (A*A^-1) is : n");
multiplication(matrix2,inverse,n);
}
}
33. Use c program to interchange two rows of a matrix with highest and lowest diagonal
elements.
#include <stdio.h>
void swap(int m[10][10],int n,int p,int q)
{
int j,temp;
for(j=0;j<n;j++)
{
temp=m[p][j];
m[p][j]=m[q][j];
m[q][j]=temp;
}
}
void print_matrix(int m[10][10],int n)
{
int i,j;
for(i=0;i<n;i++)
{
printf("n");
for(j=0;j<n;j++)
printf("%dt",m[i][j]);
}
}
int largest(int m[10][10],int n)
{
int i,large=m[0][0];
for(i=0;i<n;i++)
{
if(m[i][i]>=large)
large=m[i][i];
}
for(i=0;i<n;i++)
if(large==m[i][i])
return i;
}
int smallest(int m[10][10],int n)
{
int i,small=m[0][0];
for(i=0;i<n;i++)
{
if(m[i][i]<=small)
small=m[i][i];
32. P a g e | 32
NB : Make necessary corrections if required
}
for(i=0;i<n;i++)
if(small==m[i][i])
return i;
}
int main()
{
int n,m[10][10],i,j,temp,p,q;
printf("Enter the order of matrix:n");
scanf("%d",&n);
printf("enter the elementsn");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
scanf("%d",&m[i][j]);
}
printf("given matrix isn");
print_matrix(m,n);
p=largest(m,n);
q=smallest(m,n);
swap(m,n,p,q);
printf("nAfter swapping rows with highest & lowest diagonal elements : n");
print_matrix(m,n);
}
34. Use c program to get 2I - A, where A is a square matrix and I is an identity matrix of
order n.
#include <stdio.h>
void print_matrix(int m[10][10],int n)
{
int i,j;
for(i=0;i<n;i++)
{
printf("n");
for(j=0;j<n;j++)
printf("%d t",m[i][j]);
}
}
void subtraction(int mat1[10][10],int mat2[10][10],int n)
{
int i,j,mat3[10][10];
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
mat3[i][j]=mat1[i][j]-mat2[i][j];
}
printf("n2I-A matrix is : n");
print_matrix(mat3,n);
33. P a g e | 33
NB : Make necessary corrections if required
}
int main()
{
int n,MatA[10][10],idnt[10][10],i,j;
printf("Enter the order of A matrix:n");
scanf("%d",&n);
printf("Enter the elements of A matrix : n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&MatA[i][j]);
printf("The A matrix is : n");
print_matrix(MatA,n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(i==j)
idnt[i][j]=2;
else
idnt[i][j]=0;
}
printf("nThe 2I matrix is : n");
print_matrix(idnt,n);
subtraction(idnt,MatA,n);
}
35. Use c program to get (2I - A)/det(A), where A is a square matrix and I is an identity
matrix of order n.
#include <stdio.h>
void print_matrix(float m[10][10],int n)
{
int i,j;
for(i=0;i<n;i++)
{
printf("n");
for(j=0;j<n;j++)
printf("%f t",m[i][j]);
}
}
void RowDiv(float a[10][10],int n,float T)
{
int j,r;
for(r=0;r<n;r++)
{
for(j=0;j<n;j++)
a[r][j]=a[r][j]/T;
}
34. P a g e | 34
NB : Make necessary corrections if required
printf("nnThe Result (2I-A)/det(A) is : n");
print_matrix(a,n);
}
void subtraction(float mat1[10][10],float mat2[10][10],int n,float det)
{
int i,j;
float mat3[10][10];
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
mat3[i][j]=mat1[i][j]-mat2[i][j];
}
printf("nn2I-A matrix is : n");
print_matrix(mat3,n);
RowDiv(mat3,n,det);
}
float determinant(float matrix[10][10],int n)
{
int i,j,ratio,k;
float det=1;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i>j)
{
ratio = matrix[i][j]/matrix[j][j];
for(k=0;k<n;k++)
matrix[i][k]=matrix[i][k]-ratio*matrix[j][k];
}
}
}
for(i=0;i<n;i++)
det=det*matrix[i][i];
return det;
}
int main()
{
int n,i,j;
float MatA[10][10],det,idnt[10][10],MatB[10][10];
printf("Enter the order of A matrix:n");
scanf("%d",&n);
printf("Enter the elements of A matrix : n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%f",&MatA[i][j]);
printf("The A matrix is : n");
print_matrix(MatA,n);
35. P a g e | 35
NB : Make necessary corrections if required
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
MatB[i][j]=MatA[i][j];
if(i==j)
idnt[i][j]=2;
else
idnt[i][j]=0;
}
printf("nnThe 2I matrix is : n");
print_matrix(idnt,n);
det=determinant(MatB,n);
printf("nnDeterminant of the A matrix is : %fn",det);
subtraction(idnt,MatA,n,det);
}
36. Arrange rows of a matrix such that the sum of rows are in ascending order.
#include <stdio.h>
void print_matrix(int m[10][10],int n)
{
int i,j;
for(i=0;i<n;i++)
{
printf("n");
for(j=0;j<n;j++)
printf("%dt",m[i][j]);
}
}
void sum_of_rows(int mat[10][10],int n)
{
int i,j;
for(i=0;i<n;i++)
{
mat[i][n]=0;
for(j=0;j<n;j++)
mat[i][n]=mat[i][n]+mat[i][j];
}
printf("nThe sum of rows are : n");
for(i=0;i<n;i++)
printf("%dn",mat[i][n]);
}
void swap(int a,int b,int mt[10][10],int n)
{
int j,temp;
for(j=0;j<=n;j++)
{
temp=mt[a][j];
mt[a][j]=mt[b][j];
36. P a g e | 36
NB : Make necessary corrections if required
mt[b][j]=temp;
}
}
void ascending(int a[10][10],int m)
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<m-1;j++)
if(a[j][m]>a[j+1][m])
swap(j,j+1,a,m);
}
for(i=0;i<m;i++)
printf("%dt",a[i][m]);
}
int main()
{
int mat[10][10],n,sum[10],i,j;
printf("Enter the order of the matrix : n");
scanf("%d",&n);
printf("Enter the elements of matrix :n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&mat[i][j]);
printf("The Given Matrix is : n");
print_matrix(mat,n);
sum_of_rows(mat,n);
printf("nThe sum of rows of matrix in Ascending order is :n");
ascending(mat,n);
printf("nnAfter Arranging rows of a matrix such that the sum of rows are in
ascending order :n");
print_matrix(mat,n);
}
37. Using C program, verify that in general AB ≠ BA
#include<stdio.h>
void multiplication(int mat1[10][10],int mat2[10][10],int mat3[10][10],int n)
{
int i,j,k,sum;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
sum=0;
for(k=0;k<n;k++)
sum=sum+mat1[i][k]*mat2[k][j];
mat3[i][j]=sum;
37. P a g e | 37
NB : Make necessary corrections if required
}
}
void print_matrix(int m[10][10],int n)
{
int i,j;
for(i=0;i<n;i++)
{
printf("n");
for(j=0;j<n;j++)
printf("%dt",m[i][j]);
}
}
void check_equality(int A1[10][10],int A2[10][10],int n)
{
int i,j,f=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(A1[i][j]==A2[i][j])
f++;
}
if(f==n*n)
printf("nFrom the above results AB=BAn");
else
printf("nFrom the above results AB!=BAn");
}
int main()
{
int n,A[10][10],B[10][10],AB[10][10],BA[10][10],i,j;
printf("Enter the order of the Matrix:n");
scanf("%d",&n);
printf("Enter Elements of A matrix :n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&A[i][j]);
printf("nA matrix is : n");
print_matrix(A,n);
printf("nEnter Elements of B matrix :n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&B[i][j]);
printf("nB matrix is : n");
print_matrix(B,n);
multiplication(A,B,AB,n);
printf("nAB matrix is : n");
print_matrix(AB,n);
multiplication(B,A,BA,n);
printf("nBA matrix is : n");
print_matrix(BA,n);
38. P a g e | 38
NB : Make necessary corrections if required
check_equality(AB,BA,n);
}
38. Using c program, find + 2 where A and B are matrices.
#include <stdio.h>
void print_matrix(int d[10][10],int n)
{
int i,j;
for(i=0;i<n;i++)
{
printf("n");
for(j=0;j<n;j++)
printf("%d t",d[i][j]);
}
}
int main()
{
int i,j,n,a[10][10],b[10][10],c[10][10],d[10][10],k;
printf("Enter the order of matrix :n");
scanf("%d",&n);
printf("Enter the elements of matrix A:n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
printf("Enter the elements of matrix B:n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&b[i][j]);
c[i][j]=0;
}
}
printf("nThe A Matrix is :n");
print_matrix(a,n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
b[i][j]=2*b[i][j];
}
printf("nThe 2B Matrix is :n");
print_matrix(b,n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
for(k=0;k<n;k++)
39. P a g e | 39
NB : Make necessary corrections if required
c[i][j]=c[i][j]+a[i][k]*a[k][j];
}
printf("nThe A^2 matrix is:n");
print_matrix(c,n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
d[i][j]=c[i][j]+b[i][j];
printf("nThe Final result A^2+2B is :n");
print_matrix(d,n);
}
39. Using c program, Check whether a number is palindrom or not ?
#include<stdio.h>
int main()
{
int n, reverse=0, temp;
printf("Enter the number to check palindrome or notn");
scanf("%d",&n);
temp=n;
while(temp!=0)
{
reverse=reverse*10;
reverse=reverse+temp%10;
temp=temp/10;
}
if(n==reverse)
printf("The given number %d is palindrome numbern",n);
else
printf("The given number %d is not palindrome numbern",n);
}
40. Using c program, Check whether a string is palindrom or not ?
#include<stdio.h>
#include<string.h>
int main()
{
char a[100], b[100];
printf("Enter the word to check palindromn");
gets(a);
strcpy(b,a);
strrev(b);
if(strcmp(a,b)==0)
printf("Enterd string is a palindromen");
else
printf("Enterd string is not palindromen");
return 0;
40. P a g e | 40
NB : Make necessary corrections if required
}
41. Write C Program to find solution of a Quadratic Equation ?
#include<stdio.h>
#include<math.h>
int main()
{
float a, b, c, determinant,r1,r2,real,imag;
printf("Enter the coefficients a,b and c n");
scanf("n%f n%f n%f",&a,&b,&c);
determinant=b*b-4*a*c;
printf("Determinant is : t %f n",determinant);
if(determinant>0)
{
r1=(-b+sqrt(determinant))/(2*a);
r2=(-b-sqrt(determinant))/(2*a);
printf("Roots are: %f and %f",r1,r2);
}
else if(determinant==0)
{
r1=r2=-b/(2*a);
printf("Roots are: %f and %f",r1,r2);
}
else
{
real=-b/(2*a);
imag=sqrt(-determinant)/(2*a);
printf("Roots are : %f+j%f and t %f-j%f ",real,imag,real,imag);
}
return 0;
}
42. Use c program, to find the matrix[ … ]
#include <stdio.h>
read_matrix(float a[10][10],int r,int c)
{
int i,j;
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%f",&a[i][j]);
41. P a g e | 41
NB : Make necessary corrections if required
}
void print_matrix(float d[10][10],int r,int c)
{
int i,j;
for(i=0;i<r;i++)
{
printf("n");
for(j=0;j<c;j++)
printf("%f t",d[i][j]);
}
}
void multiplication(float a[10][10],float b[10][10],float c[10][10],int r,int d)
{
int i,j,k;
for(i=0;i<r;i++)
for(j=0;j<d;j++)
{
c[i][j]=0;
for(k=0;k<r;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
void assign(float a[10][10],float b[10][10],int r,int c)
{
int i,j;
for(i=0;i<r;i++)
for(j=0;j<c;j++)
a[i][j]=b[i][j];
}
void controllability(float a[10][10],float b[10][10],float c[10][10],float con[10][10],int r,int
d)
{
int i,j,k,m=r;
for(k=1;k<m;k++)
{
multiplication(a,b,c,r,d);
assign(b,c,r,d);
for(i=0;i<r;i++)
for(j=0;j<d;j++)
{
con[i][j+d*k]=c[i][j];
}
}
}
int main()
{
int r1,r2,c1,c2,i,k,j;
float a[10][10],b[10][10],c[10][10],con[10][10];
printf("Enter the order of A matrix (no.of rows * no.of columns) :n");
42. P a g e | 42
NB : Make necessary corrections if required
scanf("%d%d",&r1,&c1);
printf("Enter the elements of A matrix:n");
read_matrix(a,r1,c1);
printf("A matrix is : n");
print_matrix(a,r1,c1);
printf("nEnter the order of B matrix (no.of rows * no.of columns) :n");
scanf("%d%d",&r2,&c2);
printf("Enter the elements of B matrix:n");
read_matrix(b,r2,c2);
printf("nB matrix is :n");
print_matrix(b,r2,c2);
assign(con,b,r2,c2);
if(r2==c1)
controllability(a,b,c,con,r1,c2);
printf("nThe controllability matrix is :n");
print_matrix(con,r2,r1*c2);
}
43. Use c program, to find the matrix[ … ]
#include <stdio.h>
void print_matrix(float d[10][10],int r,int c)
{
int i,j;
for(i=0;i<r;i++)
{
printf("n");
for(j=0;j<c;j++)
printf("%f t",d[i][j]);
}
}
void transpose(float a[10][10],float b[10][10],int r,int c)
{
int i,j,temp;
for(i=0;i<c;i++)
for(j=0;j<r;j++)
b[i][j]=a[j][i];
}
void swap_order(int *a,int *b)
{
int temp=*a;
*a=*b;
*b=temp;
}
read_matrix(float a[10][10],int r,int c)
{
int i,j;
for(i=0;i<r;i++)
43. P a g e | 43
NB : Make necessary corrections if required
for(j=0;j<c;j++)
scanf("%f",&a[i][j]);
}
void multiplication(float a[10][10],float b[10][10],float c[10][10],int r,int d)
{
int i,j,k;
for(i=0;i<r;i++)
for(j=0;j<d;j++)
{
c[i][j]=0;
for(k=0;k<r;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
void assign(float a[10][10],float b[10][10],int r,int c)
{
int i,j;
for(i=0;i<r;i++)
for(j=0;j<c;j++)
a[i][j]=b[i][j];
}
void observability(float a[10][10],float b[10][10],float c[10][10],float obs[10][10],int r,int
d)
{
int i,j,k,m=r;
for(k=1;k<m;k++)
{
multiplication(a,b,c,r,d);
assign(b,c,r,d);
for(i=0;i<r;i++)
for(j=0;j<d;j++)
{
obs[i][j+d*k]=c[i][j];
}
}
}
int main()
{
int r1,r2,c1,c2,i,k,j;
float a[10][10],b[10][10],c[10][10],obs[10][10],aT[10][10],bT[10][10];
printf("Enter the order of A matrix (no.of rows * no.of columns) :n");
scanf("%d%d",&r1,&c1);
printf("Enter the elements of A matrix:n");
read_matrix(a,r1,c1);
printf("A matrix is : n");
print_matrix(a,r1,c1);
transpose(a,aT,r1,c1);
44. P a g e | 44
NB : Make necessary corrections if required
swap_order(&r1,&c1);
printf("nA Transpose matrix is : n");
print_matrix(aT,r1,c1);
printf("nEnter the order of B matrix (no.of rows * no.of columns) :n");
scanf("%d%d",&r2,&c2);
printf("Enter the elements of B matrix:n");
read_matrix(b,r2,c2);
printf("nB matrix is :n");
print_matrix(b,r2,c2);
transpose(b,bT,r2,c2);
swap_order(&r2,&c2);
printf("nB Transpose matrix is : n");
print_matrix(bT,r2,c2);
assign(obs,bT,r2,c2);
if(r2==c1)
observability(aT,bT,c,obs,r1,c2);
printf("nThe observability matrix is :n");
print_matrix(obs,r2,r1*c2);
}