/* Write a C program that uses functions to perform the following:

         i) Addition of Two Matrices

         ii) Multiplication of Two Matrices

*/



#include<stdio.h>



void main()

{

int ch,i,j,m,n,p,q,k,r1,c1,a[10][10],b[10][10],c[10][10];



printf("************************************");

printf("nttMENU");

printf("n**********************************");

printf("n[1]ADDITION OF TWO MATRICES");

printf("n[2]MULTIPLICATION OF TWO MATRICES");

printf("n[0]EXIT");

printf("n**********************************");

printf("ntEnter your choice:n");

scanf("%d",&ch);



if(ch<=2 & ch>0)

{

printf("Valid Choicen");
}



switch(ch)

{

    case 1:

    printf("Input rows and columns of A & B Matrix:");

    scanf("%d%d",&r1,&c1);

    printf("Enter elements of matrix A:n");

     for(i=0;i<r1;i++)

     {

         for(j=0;j<c1;j++)

         scanf("%d",&a[i][j]);

     }

    printf("Enter elements of matrix B:n");

     for(i=0;i<r1;i++)

     {

         for(j=0;j<c1;j++)

         scanf("%d",&b[i][j]);

     }

    printf("n =====Matrix Addition=====n");

     for(i=0;i<r1;i++)

     {

         for(j=0;j<c1;j++)

         printf("%5d",a[i][j]+b[i][j]);
printf("n");

    }

break;



case 2:

printf("Input rows and columns of A matrix:");

scanf("%d%d",&m,&n);

printf("Input rows and columns of B matrix:");

scanf("%d%d",&p,&q);

if(n==p)

{

printf("matrices can be multipliedn");

printf("resultant matrix is %d*%dn",m,q);

printf("Input A matrixn");

read_matrix(a,m,n);

printf("Input B matrixn");

/*Function call to read the matrix*/

read_matrix(b,p,q);

/*Function for Multiplication of two matrices*/

printf("n =====Matrix Multiplication=====n");

for(i=0;i<m;++i)

    for(j=0;j<q;++j)

    {

        c[i][j]=0;
for(k=0;k<n;++k)

                c[i][j]=c[i][j]+a[i][k]*b[k][j];

        }



    printf("Resultant of two matrices:n");

        write_matrix(c,m,q);

        }

        /*end if*/

    else

    {

    printf("Matrices cannot be multiplied.");

    }

    /*end else*/

    break;



    case 0:

    printf("n Choice Terminated");

    exit();

    break;



    default:

    printf("n Invalid Choice");

}
}



/*Function read matrix*/

int read_matrix(int a[10][10],int m,int n)

    {

        int i,j;

         for(i=0;i<m;i++)

                   for(j=0;j<n;j++)

                   scanf("%d",&a[i][j]);

    return 0;

    }



    /*Function to write the matrix*/

int write_matrix(int a[10][10],int m,int n)

    {

        int i,j;

         for(i=0;i<m;i++)

         {

                   for(j=0;j<n;j++)

                   printf("%5d",a[i][j]);

                   printf("n");

         }

    return 0;

    }
/* The total distance travelled by vehicle in 't' seconds is given by distance = ut+1/2at2

     where 'u' and 'a' are the initial velocity (m/sec.) and acceleration (m/sec2).

     Write C program to find the distance travelled at regular intervals of time given

     the values of 'u' and 'a'. The program should provide the flexibility to the user

     to select his own time intervals and repeat the calculations for different values of 'u' and 'a'.

*/



#include <stdio.h>

#include <math.h>



void main()

{

int tim_intrval, counter,time;

float accl, distance=0, velos;

clrscr();

printf("<===========PROGRAM FOR CALC TOTAL DISTANCE TRAVELED BY A
VECHIAL===========>");

printf("nnntttNO OF TIME INTERVALS : ");

scanf("%d",&tim_intrval);
for(counter = 1; counter <= tim_intrval; counter++)

{

     printf("ntttAT T%d TIME(sec) : ",counter);

     scanf("%d",&time);

     printf("tttVELOCITY AT %d sec (m/sec) : ",time);

     scanf("%f",&velos);

     printf("tttACCLERATION AT %d sec (m/sec^2): ",time);

     scanf("%f",&accl);

     distance += (velos*time + (accl*pow(time,2))/2);

}



printf("nnntTOTAL DISTANCE TRAVELLED BY VEHICLE IN %d INTERVALS OF TIME :
%f",tim_intrval,distance);

getch();

}
/* Write a C program, which takes two integer operands and one operator form the user,

    performs the operation and then prints the result.

    (Consider the operators +,-,*, /, % and use Switch Statement)

*/



#include<stdio.h>

#include<conio.h>



void main()

{

int a,b,res,ch;

clrscr();

printf("t *********************");

printf("ntMENUn");

printf("t********************");

printf("nt(1)ADDITION");
printf("nt(2)SUBTRACTION");

printf("nt(3)MULTIPLICATION");

printf("nt(4)DIVISION");

printf("nt(5)REMAINDER");

printf("nt(0)EXIT");

printf("nt********************");

printf("nntEnter your choice:");

scanf("%d",&ch);



if(ch<=5 & ch>0)

{

printf("Enter two numbers:n");

scanf("%d%d",&a,&b);

}



switch(ch)

{

case 1:

res=a+b;

printf("n Addition:%d",res);

break;



case 2:

res=a-b;
printf("n Subtraction:%d",res);

break;



case 3:

res=a*b;

printf("n Multiplication:%d",res);

break;



case 4:

res=a/b;

printf("n Division:%d",res);

break;



case 5:

res=a%b;

printf("n Remainder:%d",res);

break;



case 0:

printf("n Choice Terminated");

exit();

break;



default:
printf("n Invalid Choice");

}

getch();

}

week-3x

  • 1.
    /* Write aC program that uses functions to perform the following: i) Addition of Two Matrices ii) Multiplication of Two Matrices */ #include<stdio.h> void main() { int ch,i,j,m,n,p,q,k,r1,c1,a[10][10],b[10][10],c[10][10]; printf("************************************"); printf("nttMENU"); printf("n**********************************"); printf("n[1]ADDITION OF TWO MATRICES"); printf("n[2]MULTIPLICATION OF TWO MATRICES"); printf("n[0]EXIT"); printf("n**********************************"); printf("ntEnter your choice:n"); scanf("%d",&ch); if(ch<=2 & ch>0) { printf("Valid Choicen");
  • 2.
    } switch(ch) { case 1: printf("Input rows and columns of A & B Matrix:"); scanf("%d%d",&r1,&c1); printf("Enter elements of matrix A:n"); for(i=0;i<r1;i++) { for(j=0;j<c1;j++) scanf("%d",&a[i][j]); } printf("Enter elements of matrix B:n"); for(i=0;i<r1;i++) { for(j=0;j<c1;j++) scanf("%d",&b[i][j]); } printf("n =====Matrix Addition=====n"); for(i=0;i<r1;i++) { for(j=0;j<c1;j++) printf("%5d",a[i][j]+b[i][j]);
  • 3.
    printf("n"); } break; case 2: printf("Input rows and columns of A matrix:"); scanf("%d%d",&m,&n); printf("Input rows and columns of B matrix:"); scanf("%d%d",&p,&q); if(n==p) { printf("matrices can be multipliedn"); printf("resultant matrix is %d*%dn",m,q); printf("Input A matrixn"); read_matrix(a,m,n); printf("Input B matrixn"); /*Function call to read the matrix*/ read_matrix(b,p,q); /*Function for Multiplication of two matrices*/ printf("n =====Matrix Multiplication=====n"); for(i=0;i<m;++i) for(j=0;j<q;++j) { c[i][j]=0;
  • 4.
    for(k=0;k<n;++k) c[i][j]=c[i][j]+a[i][k]*b[k][j]; } printf("Resultant of two matrices:n"); write_matrix(c,m,q); } /*end if*/ else { printf("Matrices cannot be multiplied."); } /*end else*/ break; case 0: printf("n Choice Terminated"); exit(); break; default: printf("n Invalid Choice"); }
  • 5.
    } /*Function read matrix*/ intread_matrix(int a[10][10],int m,int n) { int i,j; for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&a[i][j]); return 0; } /*Function to write the matrix*/ int write_matrix(int a[10][10],int m,int n) { int i,j; for(i=0;i<m;i++) { for(j=0;j<n;j++) printf("%5d",a[i][j]); printf("n"); } return 0; }
  • 6.
    /* The totaldistance travelled by vehicle in 't' seconds is given by distance = ut+1/2at2 where 'u' and 'a' are the initial velocity (m/sec.) and acceleration (m/sec2). Write C program to find the distance travelled at regular intervals of time given the values of 'u' and 'a'. The program should provide the flexibility to the user to select his own time intervals and repeat the calculations for different values of 'u' and 'a'. */ #include <stdio.h> #include <math.h> void main() { int tim_intrval, counter,time; float accl, distance=0, velos; clrscr(); printf("<===========PROGRAM FOR CALC TOTAL DISTANCE TRAVELED BY A VECHIAL===========>"); printf("nnntttNO OF TIME INTERVALS : "); scanf("%d",&tim_intrval);
  • 7.
    for(counter = 1;counter <= tim_intrval; counter++) { printf("ntttAT T%d TIME(sec) : ",counter); scanf("%d",&time); printf("tttVELOCITY AT %d sec (m/sec) : ",time); scanf("%f",&velos); printf("tttACCLERATION AT %d sec (m/sec^2): ",time); scanf("%f",&accl); distance += (velos*time + (accl*pow(time,2))/2); } printf("nnntTOTAL DISTANCE TRAVELLED BY VEHICLE IN %d INTERVALS OF TIME : %f",tim_intrval,distance); getch(); }
  • 8.
    /* Write aC program, which takes two integer operands and one operator form the user, performs the operation and then prints the result. (Consider the operators +,-,*, /, % and use Switch Statement) */ #include<stdio.h> #include<conio.h> void main() { int a,b,res,ch; clrscr(); printf("t *********************"); printf("ntMENUn"); printf("t********************"); printf("nt(1)ADDITION");
  • 9.
  • 10.
    printf("n Subtraction:%d",res); break; case 3: res=a*b; printf("nMultiplication:%d",res); break; case 4: res=a/b; printf("n Division:%d",res); break; case 5: res=a%b; printf("n Remainder:%d",res); break; case 0: printf("n Choice Terminated"); exit(); break; default:
  • 11.