↓↑ Program to find second maximum element in array.
#include<stdio.h>
int main()
{
// int arr[]={15,50,45,30,50};// here max1=50 max2=50
int arr[]={15,50,45,30,5}; //here max1=50 max2=45
int i,max1,max2,len=sizeof(arr)/sizeof(arr[0]);
if(arr[0]>arr[1]){
max1=arr[0];
max2=arr[1];
}else{
max1=arr[1];
max2=arr[0];
}
for(i=2;i<len;i++)
{
if(max1<arr[i]){
max2=max1;
max1=arr[i];
}else if(max2<arr[i] /*&& max1>arr[i]*/){
max2=arr[i];
}
else{}
}
printf("max1=%d max2=%d \n",max1,max2);
return 0;
}
↓↑ Program for number system(Binary,octal,hexadecimal,decimal)conversion.
#include<stdio.h>
int main()
{
int a[32],b[32],i,j,choice,n,count=0;
char ch=' ';
do{
printf("************************************\n");
printf("1.Decimal to binary.\n");
printf("2.decimal to octal.\n");
printf("3.decimal to hexadecimal.\n");
printf("4.binary to decimal.\n");
printf("5.binary to octal\n");
printf("6.binary to hexadecimal.\n");
printf("7.EXIT\n****************************\n");
printf("Enter your choice:");
scanf("%d",&choice);
long int d,rem,n1;
if(choice<1 && choice>7)
{
choice=0;
}
switch(choice)
{
case 1:
printf("\nEnter decimal No. :");
scanf("%d",&n);
i=0;
while(n)
{
a[i]=n%2;
n=n/2;
i++;
count++;
}
for(j=0,i=count-1;j<count,i>=0;j++,i--)
{
b[j]=a[i];
}
printf("Binary no is:");
for(i=0;i<count;i++){
printf("%d",b[i]);}
printf("\n");
break;
case 2:
printf("\nEnter decimal No. :");
scanf("%d",&n);
i=0;
while(n)
{
a[i]=n%8;
n=n/8;
i++;
count++;
}
for(j=0,i=count-1;j<count,i>=0;j++,i--)
{
b[j]=a[i];
}
printf("octal no is:");
for(i=0;i<count;i++){
printf("%d",b[i]);
}
printf("\n");
break;
case 3:
printf("\nEnter decimal No. :");
scanf("%d",&n);
i=0;
while(n)
{
a[i]=n%16;
n=n/16;
i++;
count++;
}
for(j=0,i=count-1;j<count,i>=0;j++,i--)
{
b[j]=a[i];
}
printf("hexadcimal no is:");
for(i=0;i<count;i++)
{
if(b[i]==10||b[i]==11||b[i]==12||b[i]==13||b[i]==14||b[i]==15)
printf("%c",b[i]+55);
else
printf("%d",b[i]);
}
printf("\n");
break;
case 4:
j=1;d=0;
printf("Enter any binary number: ");
scanf("%ld",&n1);
while(n1!=0){
rem=n1%10;
d=d+rem*j;
j=j*2;
n1=n1/10;
}
printf("Equivalent decimal value: %ld\n",d);
break;
case 5:
j=1;d=0;
printf("Enter any binary number: ");
scanf("%ld",&n1);
while(n1!=0){
rem=n1%10;
d=d+rem*j;
j=j*2;
n1=n1/10;
}
printf("Equivalent octal value: %lo\n",d);
break;
case 6:
j=1;d=0;
printf("Enter any binary number: ");
scanf("%ld",&n1);
while(n1!=0){
rem=n1%10;
d=d+rem*j;
j=j*2;
n1=n1/10;
}
printf("Equivalent hexadecimal value: %lx\n",d);
break;
case 7:
break;
default:
printf("Wrong choice !!! enter choice any of(1 to 7)");
}
fflush(stdin);
printf("want to continue enter y/n :");
scanf(" %c",&ch);
}while(ch=='y'||ch=='Y');
return 0;
}
↓↑ Program to implement cramer's rule to compute two unknown.
#include<stdio.h>
float det(float[2][2],int);
int main()
{
int n=2,a1,b1,c1,a2,b2,c2,a3,b3,c3;
printf("Enter the coefficient a1,b1,c1 (a1x+b1y=c1)of first equation: \n");
scanf("%d%d%d",&a1,&b1,&c1);
printf("Enter the coefficient a2,b2,c2 (a2x+b2y=c2)of second equation: \n");
scanf("%d%d%d",&a2,&b2,&c2);
float m[2][2]={a1,b1,a2,b2},x1[2][2]={c1,b1,c2,b2},y1[2][2]={a1,c1,a2,c2},x,y;
x=(det(x1,n))/(det(m,n));
y=(det(y1,n))/(det(m,n));
printf("x=%f\ty=%f\n",x,y);
return 0;
}
float det(float matrix[2][2],int n)
{
int i, j, k;
float det,ratio;
for(i = 0; i < n; i++){
for(j = 0; j < n; j++){
if(j>i){
ratio = matrix[j][i]/matrix[i][i];
for(k = 0; k < n; k++){
matrix[j][k] -=ratio * matrix[i][k];
}
}
}
}
det = 1; //storage for determinant
for(i = 0; i < n; i++)
det *= matrix[i][i];
//printf("The determinant of matrix is: %.2f\n\n", det);
return det;
}
/*output:
* 4x-3y=11
* 6x+5y=7
Enter the coefficient a1,b1,c1 (a1x+b1y=c1)of first equation:
4
-3
11
Enter the coefficient a2,b2,c2 (a2x+b2y=c2)of second equation:
6
5
7
x=2.000000 y=-1.000000
*/
↓↑ Program to implement cramer's rule to compute three unknown.
#include<stdio.h>
float det(float[3][3],int);
int main()
{
int n=3,a1,b1,c1,d1,a2,b2,c2,d2,a3,b3,c3,d3;
printf("Enter the coefficient a1,b1,c1,d1 (a1x+b1y+c1z=d1) of first equation: \n");
scanf("%d%d%d%d",&a1,&b1,&c1,&d1);
printf("Enter the coefficient a2,b2,c2,d2 (a2x+b2y+c2z=d2)of second equation: \n");
scanf("%d%d%d%d",&a2,&b2,&c2,&d2);
printf("Enter the coefficient a2,b2,c2,d2 (a3x+b3y+c3z=d3)of second equation: \n");
scanf("%d%d%d%d",&a3,&b3,&c3,&d3);
float m[3][3]={a1,b1,c1,a2,b2,c2,a3,b3,c3},x1[3][3]={d1,b1,c1,d2,b2,c2,d3,b3,c3},y1[3][3]={a1,d1,c1,a2,d2,c2,a3,d3,c3},z1[3][3]={a1,b1,d1,a2,b2,d2,a3,b3,d3},x,y,z;
x=(det(x1,n))/(det(m,n));
y=(det(y1,n))/(det(m,n));
z=(det(z1,n))/(det(m,n));
printf("x=%.2f\ty=%.2f\tz=%.2f\n",x,y,z);
return 0;
}
float det(float matrix[3][3],int n)
{
int i, j, k;
float det,ratio;
for(i = 0; i < n; i++){
for(j = 0; j < n; j++){
if(j>i){
ratio = matrix[j][i]/matrix[i][i];
for(k = 0; k < n; k++){
matrix[j][k] -=ratio * matrix[i][k];
}
}
}
}
det = 1;
for(i = 0; i < n; i++)
det *= matrix[i][i];
return det;
}
/*Output:
* x+2y+3z=-5
* 3x+y-3z=4
* -3x+4y+7z=-7
Enter the coefficient a1,b1,c1,d1 (a1x+b1y+c1z=d1) of first equation:
1
2
3
-5
Enter the coefficient a2,b2,c2,d2 (a2x+b2y+c2z=d2)of second equation:
3
1
-3
4
Enter the coefficient a2,b2,c2,d2 (a3x+b3y+c3z=d3)of second equation:
-3
4
7
-7
x=-1.00 y=1.00 z=-2.00
*/
↓↑ Program to calculate determent of matrix connverting to upper triangular matrix.
#include<stdio.h>
int main()
{
float matrix[10][10], ratio, det;
int i, j, k, n;
clrscr();
printf("Enter 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]);
}
}
// Conversion of matrix to upper triangular
for(i = 0; i < n; i++){
for(j = 0; j < n; j++){
if(j>i){
ratio = matrix[j][i]/matrix[i][i];
for(k = 0; k < n; k++){
matrix[j][k] -=ratio * matrix[i][k];
}
}
}
}
det = 1; //storage for determinant
for(i = 0; i < n; i++)
det *= matrix[i][i];
printf("The determinant of matrix is: %.2f\n\n", det);
return 0;
}
↓↑ Program to generate Identity matrix.
#include<stdio.h>
int main()
{
int a[5][5],i,j,r,c;
printf("enter rows and columns of matrix:");
scanf("%d %d",&r,&c);
if(r==c)
{
printf("\nenter element of %dX%d matrix:",r,c);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\nentered matrix is ");
for(i=0;i<r;i++)
{
printf("\n");
for(j=0;j<c;j++)
{
printf("%d\t",a[i][j]);
}
}
printf("\nidentity matrix corresponding to given matrix is:\n");
for(i=0;i<r;i++)
{
printf("\n");
for(j=0;j<c;j++)
{
if(i==j)
{
a[i][j]=1;
printf("%d\t",a[i][j]);
}
else
{
a[i][j]=0;
printf("%d\t",a[i][j]);
}
}
}
printf("\n");
}
else
{
printf("to generate identity matrix rows and columns should be equal\n");
}
return 0;
}
↓↑ Program to find minimum and maximum element in an array.
#include<stdio.h>
int main()
{
int i,a[7],max=0,min=32432;
printf("enter elements of array:");
for(i=0;i<7;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<7;i++)
{
if(a[i]>max)
{
max=a[i];
}
if(a[i]<min)
{
min=a[i];
}
}
printf("\nmaximum value is:%d\nminimum value is:%d\n",max,min);
return 0;
}
↓↑ Program to Implement matrix multiplication.
#include<stdio.h>
int main()
{
int a[3][3],b[3][3],c[3][3],i,j,k,sum=0,m,n,o,p;
printf("\nEnter the row and column of first matrix:");
scanf("%d %d",&m,&n);
printf("\nEnter the row and column of second matrix:");
scanf("%d %d",&o,&p);
if(n!=o)
{
printf("Matrix mutiplication is not possible");
printf("\nColumn of first matrix must be same as row of second matrix");
}
else
{
printf("\nEnter the First matrix->");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\nEnter the Second matrix->");
for(i=0;i<o;i++)
for(j=0;j<p;j++)
scanf("%d",&b[i][j]);
printf("\nThe First matrix is\n");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
}
printf("\nThe Second matrix is\n");
for(i=0;i<o;i++)
{
printf("\n");
for(j=0;j<p;j++)
{
printf("%d\t",b[i][j]);
}
}
for(i=0;i<m;i++)
for(j=0;j<p;j++)
c[i][j]=0;
for(i=0;i<m;i++)
{ //row of first matrix
for(j=0;j<p;j++)
{ //column of second matrix
sum=0;
for(k=0;k<n;k++)
sum=sum+a[i][k]*b[k][j];
c[i][j]=sum;
}
}
}
printf("\nThe multiplication of two matrix is\n");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<p;j++)
{
printf("%d\t",c[i][j]);
}
}
printf("\n");
return 0;
}
↓↑ Program to generate null matrix.
#include<stdio.h>
int main()
{
int a[5][5],b[5][5],i,j,r,c;
printf("enter rows and columns of matrix:");
scanf("%d%d",&r,&c);
printf("\nEnter the element of a matrix");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\nEntered matrix is:\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("\nNull matrix corresponding to given is:");
for(i=0;i<r;i++)
{
printf("\n");
for(j=0;j<c;j++)
{
b[i][j]=a[i][j]-a[i][j];
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
printf("\n");
return 0;
}
↓↑ Program for pointer arithmetic.
#include<stdio.h>
int main()
{
int x=3,*p,*p1,*p2;
printf("intiger pointer\n");
p=&x;
printf("p=%u\n",p);
p1=p+5;
printf("p1=%u\n",p1);
p2=p-5;
printf("p2=%u\n",p2);
printf("\np1++=%u \np2--=%u\np1-p2=%u\n",p1++,p2--,p1-p2);
printf("\nfloat pointer\n");
float y=3.2,*q,*q1,*q2;
q=&y;
printf("q=%u\n",q);
q1=q+5;
printf("q1=%u\n",q1);
q2=q-5;
printf("q2=%u\n",q2);
printf("\nq1++=%u \nq2--=%u\nq1-q2=%u\n",q1++,q2--,q1-q2);
printf("\ncharacter pointer\n");
char r='3',*c,*c1,*c2;
c=&r;
printf("c=%u\n",c);
c1=c+5;
printf("c1=%u\n",c1);
c2=c-5;
printf("c2=%u\n",c2);
printf("c1++=%u \nc2--=%u\nc1-c2=%u\n",c1++,c2--,c1-c2);
return 0;
}
/* //output
ntiger pointer
p=106569296
p1=106569316
p2=106569276
p1++=106569316
p2--=106569276
p1-p2=10
float pointer
q=106569300
q1=106569320
q2=106569280
q1++=106569320
q2--=106569280
q1-q2=10
character pointer
c=106569295
c1=106569300
c2=106569290
c1++=106569300
c2--=106569290
c1-c2=10
*/
↓↑ Program to reverse element of array .
#include<stdio.h>
#include<math.h>
int main()
{
int i,a[]={12,10,11,33,23,13},len=sizeof(a)/sizeof(a[0]),temp;
double alen =ceil((double)len/2);
printf("\nArray before reverse: \n");
for(i=0;i<len;i++)
{
printf("%d ",a[i]);
}
for(i=0;i<(int)alen;i++)
{
temp = a[i];
a[i] = a[len-i-1];
a[len-i-1] =temp;
}
printf("\nArray after reverse: ");
for(i=0;i<len;i++)
{
printf("%d ",a[i]);
}
printf("\n");
return 0;
}
// compile with math library $gcc test.c -lm
↓↑ Program to print sum and average of array elements.
#include<stdio.h>
int main()
{
int sum=0,avg,i,a[7];
printf("enter elements of array:");
for(i=0;i<7;i++)
{
scanf("%d",&a[i]);
}
printf("\nthe array is:");
for(i=0;i<7;i++)
{
printf("%d ",a[i]);
}
printf("\nsum of array element is :");
for(i=0;i<7;i++)
{
sum+=a[i];
}
printf("%d",sum);
printf("\naverage of array element is:%d\n",sum/7);
return 0;
}
↓↑ Program to find trace of matrix.
#include<stdio.h>
int main()
{
int a[5][5],i,j,sum=0,m,n;
printf("\nEnter the row and column of matrix: ");
scanf("%d %d",&m,&n);
printf("\nEnter the elements of matrix: ");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\nThe matrix is\n");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<m;j++)
{
printf("%d\t",a[i][j]);
}
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(i==j)
sum=sum+a[i][j];
}
}
printf("\n\ntrace of a matrix is: %d",sum);
printf("\n");
return 0;
}
↓↑ Program to find transpose of matrix.
#include<stdio.h>
int main()
{
int a[6][6],b[6][6],i,j,k=0,m,n;
printf("\nEnter the row and column of matrix:");
scanf("%d %d",&m,&n);
printf("\nEnter the First matrix->");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\nThe matrix is\n");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<m;j++)
{
printf("%d\t",a[i][j]);
}
}
for(i=0;i<m;i++)
for(j=0;j<n;j++)
b[i][j]=0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
b[i][j]=a[j][i];
}
}
printf("\n\nTraspose of a matrix is -> ");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<m;j++)
{
printf("%d\t",b[i][j]);
}
}
printf("\n");
return 0;
}
↓↑ Program to check that a given matrix is identity matrix or not.
#include<stdio.h>
int main()
{
int a[5][5],b[5][5],i,j,r,c,count=0,count1=0;
printf("enter rows and columns of matrix:");
scanf("%d%d",&r,&c);
printf("\nEnter the element of a matrix");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\nEntered matrix is:\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
for(i=0;i<r;i++)
{
printf("\n");
for(j=0;j<c;j++)
{
if(i==j && (a[i][j]==1))
{
++count;
}
else if(i!=j && a[i][j]==0)
{
++count1;
}
}
}
if(count==r && count1==(r*c-r))
{
printf("\nmatrix is identity");
}
else
printf("\nmatrix is not identity");
printf("\n");
return 0;
}
↓↑ Program to check null matrix.
#include<stdio.h>
int main()
{
int a[5][5],b[5][5],i,j,r,c,count=0;
printf("enter rows and columns of matrix:");
scanf("%d%d",&r,&c);
printf("\nEnter the element of a matrix");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\nEntered matrix is:\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
for(i=0;i<r;i++)
{
printf("\n");
for(j=0;j<c;j++)
{
if(a[i][j]==0)
{
++count;
}
}
}
if(count==r*c)
{
printf("\nmatrix is null");
}
else
printf("\nmatrix is not null");
printf("\n");
return 0;
}