Sunday, 18 August 2019

fibonocci series- program


/*ratio of consecutive terms of fibonocci series*/
#include<stdio.h>
#include<math.h>
void
main()
{
                int i,f[100];
                float r0,r1;
                i=0;
                f[0]=1;
                f[1]=1;
                r1=1;
label:
                r0=r1;
                i++;
                f[i+1]=f[i]+f[(i-1)];
                r1=f[i+1]/f[i];
                if(fabs(r1-r0)>0.001)
                                goto label;
                printf("the value of the ratio is %f\n",r1);
}
               
RESULT:
 enter the number n=10
1.000000  1.0000002.000000
3.000000
5.000000
8.000000
13.000000
21.000000
34.000000
55.000000
 s=1.617647
Press any key to continue

Trace of a matrix - C program


/*trace of a matrix*/
#include<stdio.h>
#include<math.h>
void
main()
{
                int i,j,a[20][20];
                float s=0.0;
                printf("enter the elements of input matrix:");
                for(i=0;i<2;i++)
                {
                                for(j=0;j<2;j++)
                                                scanf("%d",&a[i][j]);
                }
                printf("the input matrix is:");
                printf("\n");
                for(i=0;i<2;i++)
                {
                                for(j=0;j<2;j++)
                                                printf("%d\t",a[i][j]);
                                printf("\n");
                }
                for(i=0;i<2;i++)
                {
                                for(j=0;j<2;j++)
                                {
                                                if(i==j)
                                                                s=s+a[i][j];
                                }
                }
                printf("\n the trace is=%f\n",s);
}
RESULT:
enter the elements of input matrix:1 2 3 4
the input matrix is:
1       2
3       4

 the trace is=5.000000
Press any key to continue

C program for calculation of mean, median, mode


/*calculation of mean,median,mode*/
#include<stdio.h>
void
main()
{
                int i,j,n;
                float a[100],t,sum=0.0,mean,median,mode;
                printf("enter the total number of data");
                scanf("%d",&n);
                printf("type the input data:\n");
                for(i=0;i<n;i++)
                {
                                scanf("%f",&a[i]);
                                sum=sum+a[i];
                }
                mean=sum/n;
                printf("mean=%f\n",mean);
                /*sorting array*/
                for(j=0;j<n;j++)
                {
                                for(i=0;i<n;i++)
                                {
                                                if(a[i]>a[j])
                                                {t=a[i];
                                                a[i]=a[j];
                                                a[j]=t;
                                                }
                                }
                }
                if(n%2==0)
                                median=(a[n/2]+a[(n-2)/2])/2;
                else
                                median=a[(n-1)/2];
                mode=(3*median)-(2*mean);
                printf("median=%f\n",median);
                printf("mode=%f\n",mode);
}
RESULT1:
enter the total number of data3
type the input data:
1
4
3
mean=2.666667
median=3.000000
mode=3.666667
Press any key to continue
RESULT2:
enter the total number of data 10
type the input data:
2
4
6
5
8
1
0
15
66
40
mean=14.700000
median=5.500000
mode=-12.900000
Press any key to continue
RESULT3:
enter the total number of data6
type the input data:
2
25
13
7
21
4
mean=12.000000
median=10.000000
mode=6.000000
Press any key to continue

Saturday, 17 August 2019

C program for inverse of a matrix


/*Inverse of a matrix*/
#include<stdio.h>
#include<math.h>
void
main()
{
                int i,j,det,k,a[20][20];
                float I[20][20],U[20][20];
                printf("enter the elements of input matrix:");
                for(i=0;i<2;i++)
                {
                                for(j=0;j<2;j++)
                                                scanf("%d",&a[i][j]);
                }
                printf("the input matrix is:");
                printf("\n");
                for(i=0;i<2;i++)
                {
                                for(j=0;j<2;j++)
                                                printf("%d\t",a[i][j]);
                                printf("\n");
                }
                /*matrix determinant*/
                det=(a[0][0]*a[1][1])-(a[0][1]*a[1][0]);
                printf("\n The determinant is=%d\n",det);
                /*matrix inverse*/
                if(det==0)
                                printf("The matrix inverse is not possible:");
                else
                {
                                for(i=0;i<2;i++)
                                {
                                                for(j=0;j<2;j++)
                                                {
                                                                if(i==j)
                                                                {
                                                                                I[i][j]=(a[(i+1)%2][(j+1)%2]);
                                                                }
                                                                else
                                                                {
                                                                                I[i][j]=-(a[i][j]);
                                                                }
                                                }
                                }
                                printf("The inverse matrix is \n");
                                for(i=0;i<2;i++)
                                {
                                                for(j=0;j<2;j++)
                                                                printf("%.4f\t",(I[i][j]/det));
                                                printf("\n");
                                }
                                /*product matrix*/
                                for(i=0;i<2;i++)
                                {for(j=0;j<2;j++)
                               
                                {
                                                U[i][j]=0;
                                                for(k=0;k<2;k++)
                                                                U[i][j]=U[i][j]+a[i][k]*I[k][j];
                                }
                                }
                                printf("\n the product matrix is:\n");
                                for(i=0;i<2;i++)
                                {
                                                for(j=0;j<2;j++)
                                                                printf("%.4f\t",(U[i][j]/det));
                                                printf("\n");
                                }
                }
}
RESULT 1:
enter the elements of input matrix:1 2 3 4
the input matrix is:
1       2
3       4
 The determinant is=-2
The inverse matrix is
-2.0000 1.0000
1.5000  -0.5000
 the product matrix is:
1.0000  0.0000
0.0000  1.0000
Press any key to continue
RESULT 2:
enter the elements of input matrix:1 1 1 1
the input matrix is:
1       1
1       1
 The determinant is=0
The matrix inverse is not possible:
Press any key to continue

Solution of quadratic equation C program


/*Solution of quadratic equation*/


#include<stdio.h>
#include<math.h>
void
main()
{
            float a,b,c,d,x1,x2,rp,imp;
            printf("enter the value of a,b,c");
            scanf("%f%f%f",&a,&b,&c);
            d=b*b-4.0*a*c;
            if (d>=0)
            {
                        printf("roots are real");
                        x1=(-b+sqrt(d))/(2.0*a);
                        x2=(-b-sqrt(d))/(2.0*a);
                        printf("\n x1= %f,x2= %f\n",x1,x2);
            }
            else
            {
                        printf("roots are complex");
            rp=-b*(2.0*a);
            imp=sqrt(abs(d))/(2.0*a);
            printf("first root = %f+ %fi\n",rp,imp);
            printf("second root= %f-%fi\n",rp,imp);
            }
}




/*Solution of quadratic equation */
#include<stdio.h>
#include<math.h>
void
main()
{
                float a,b,c,d,x1,x2,rp,imp;
                printf(" enter the value of a,b,c");
                scanf("%f%f%f",&a,&b,&c);
                d=(b*b)-(4.0*a*c);
                if(d>=0)
                {
                                printf("roots are real");
                                x1=(-b+sqrt(d))/(2.0*a);
                                x2=(b-sqrt(d))/(2.0*a);
                                printf("\n x1=%f,x2=%f\n",x1,x2);
                }
                else
                {
                                printf("roots are complex");
                                rp=-b/(2.0*a);
                                imp=sqrt(abs(d))/(2.0*a);
                                printf("1st root=%f+%fi\n",rp,imp);
                                printf("2nd root=%f-%fi\n",rp,imp);
                }
}
Result1:
enter the value of a,b,c2 3 1
roots are real
 x1=-0.500000,x2=0.500000
Press any key to continue
Result2:
enter the value of a,b,c2 4 2
roots are real
 x1=-1.000000,x2=1.000000
Press any key to continue
Result3:
enter the value of a,b,c2 3 5
roots are complex1st root=-0.750000+1.391941i
2nd root=-0.750000-1.391941i

Press any key to continue

Friday, 16 August 2019

Fortran program for celcius to farenheit scale


!program for celcius to farenheit scale
      write(*,*)"enter the temperature in celcius scale"
      read(*,*)c
      f=(9*c)/5 +32
      write(*,*)"the temperature in farenheit scale is"
      write(*,*)
      stop
      end
      !progrm for sum
      read(*,*)a,b,c
      y=a+b+c
      write(*,*)y
      stop
      end
! sum for n numbers
 read (*,*) n
        sum=0
        do i=1,n
          sum=sum+i
          end do
          write (*,*) sum
          stop
          end
       !sum of odd numbers
       read(*,*)n
                x=0
                do i=1,n,2
                x=x+i    
                end do
                write(*,*)
                stop
                end

C program for zeta function


/*Zeta function*/
#include<stdio.h>
#include<math.h>
void
main()
{
                int i;
                float s=0.0,pi,d;
                for(i=1;i<=4;i++)
                {
                                s=s+pow(i,-4);
                }
                d=s*90;
                pi=pow(d,0.25);
                printf("\n the value of pi is=%f\n",pi);
}
RESULT:
 the value of pi is=3.138998
Press any key to continue

C programming for addition, subtraction, multiplication of matrices


/*Addition and subtraction of matrices */
#include<stdio.h>
void
 main()
{
                int i,j,m,n,a[20][20],b[20][20],c[20][20],d[20][20];
                printf("Enter the order of matrices:");
                scanf("%d  %d",&m,&n);
                printf("Enter the element of matrix A:");
                for(i=0;i<m;i++)
                {
                                for(j=0;j<n;j++)
                                                scanf("%d",&a[i][j]);
                                printf("\n");
                }
                printf("the input matrix A is:\n");
                for(i=0;i<m;i++)
                {
                                for(j=0;j<n;j++)
                                                printf("%d\t",a[i][j]);
                                printf("\n");
                }
                                printf("Enter the element of matrix B:");
                for(i=0;i<m;i++)
                {
                                for(j=0;j<n;j++)
                                                scanf("%d",&b[i][j]);
                                printf("\n");
                }
                printf("the input matrix B is:\n");
                for(i=0;i<m;i++)
                {
                                for(j=0;j<n;j++)
                                                printf("%d\t",b[i][j]);
                                printf("\n");
                }
                /*matrix addition*/
                for(i=0;i<m;i++)
                {
                                for(j=0;j<n;j++)
                                                c[i][j]=a[i][j]+b[i][j];
                }
                printf("the summation matrix is:\n");
                for(i=0;i<m;i++)
                {
                                for(j=0;j<n;j++)
                                                printf("%d\t",c[i][j]);
                                printf("\n");
                }
                /*matrix subtraction*/
                for(i=0;i<m;i++)
                {
                                for(j=0;j<n;j++)
                                                d[i][j]=a[i][j]-b[i][j];
                }
                printf("the summation matrix is:\n");
                for(i=0;i<m;i++)
                {
                                for(j=0;j<n;j++)
                                                printf("%d\t",d[i][j]);
                                printf("\n");
                }
}

RESULT:
Enter the order of matrices:3 3
Enter the element of matrix A:1 2 3 4 5 6 7 8 9
the input matrix A is:
1       2       3
4       5       6
7       8       9
Enter the element of matrix B:12
4 56 11 7 45 0 4 6
the input matrix B is:
12      4       56
11      7       45
0       4       6
the summation matrix is:
13      6       59
15      12      51
7       12      15
the summation matrix is:
-11     -2      -53
-7      -2      -39
7       4       3
Press any key to continue
/*multiplication of matrices*/
#include<stdio.h>
void
 main()
{
                int i,j,m,n,p,k,a[20][20],b[20][20],c[20][20];
                printf("Enter the order of matrix A:");
                scanf("%d  %d",&m,&n);
                printf("Enter the order of matrix B:");
                scanf("%d  %d",&n,&p);
                printf("Enter the element of matrix A:");
                for(i=0;i<m;i++)
                {
                                for(j=0;j<n;j++)
                                                scanf("%d",&a[i][j]);
                                printf("\n");
                }
                printf("the input matrix A is:\n");
                for(i=0;i<m;i++)
                {
                                for(j=0;j<n;j++)
                                                printf("%d\t",a[i][j]);
                                printf("\n");
                }
                printf("Enter the element of matrix B:");
                for(i=0;i<n;i++)
                {
                                for(j=0;j<p;j++)
                                                scanf("%d",&b[i][j]);
                                printf("\n");
                }
                printf("the input matrix B is:\n");
                for(i=0;i<n;i++)
                {
                                for(j=0;j<p;j++)
                                                printf("%d\t",b[i][j]);
                                printf("\n");
                }
                /*matrix multiplication*/
                for(i=0;i<m;i++)
                {
                                for(j=0;j<p;j++)
                                {
                                                c[i][j]=0;
                                                for(k=0;k<n;k++)
                                                {
                                                                c[i][j]=c[i][j]+a[i][k]*b[k][j];
                                                }
                                }
                }
                printf("the multiplication matrix is :\n");
                for(i=0;i<m;i++)
                {
                                for(j=0;j<p;j++)
                                                printf("%d\t",c[i][j]);
                                printf("\n");
                }
}
RESULT:
Enter the order of matrix A:3 2
Enter the order of matrix B:2 3
Enter the element of matrix A:1 2 3 4 5 6
the input matrix A is:
1       2
3       4
5       6
Enter the element of matrix B:45 65 33 11 0 5
the input matrix B is:
45      65      33
11      0       5
the multiplication matrix is :
67      65      43
179     195     119
291     325     195
Press any key to continue