SlideShare a Scribd company logo
1 of 11
/* 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();

}

More Related Content

What's hot

C programming Lab 1
C programming Lab 1C programming Lab 1
C programming Lab 1Zaibi Gondal
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given numberMainak Sasmal
 
Lab manualsahu[et&amp;t]
Lab manualsahu[et&amp;t]Lab manualsahu[et&amp;t]
Lab manualsahu[et&amp;t]Vivek Kumar Sinha
 
C- Programming Assignment 4 solution
C- Programming Assignment 4 solutionC- Programming Assignment 4 solution
C- Programming Assignment 4 solutionAnimesh Chaturvedi
 
Chapter 5 exercises Balagurusamy Programming ANSI in c
Chapter 5 exercises Balagurusamy Programming ANSI  in cChapter 5 exercises Balagurusamy Programming ANSI  in c
Chapter 5 exercises Balagurusamy Programming ANSI in cBUBT
 
C programming Lab 2
C programming Lab 2C programming Lab 2
C programming Lab 2Zaibi Gondal
 
C- Programming Assignment 3
C- Programming Assignment 3C- Programming Assignment 3
C- Programming Assignment 3Animesh Chaturvedi
 
Chapter 5 Balagurusamy Programming ANSI in c
Chapter 5 Balagurusamy Programming ANSI  in cChapter 5 Balagurusamy Programming ANSI  in c
Chapter 5 Balagurusamy Programming ANSI in cBUBT
 
C important questions
C important questionsC important questions
C important questionsJYOTI RANJAN PAL
 
C programs
C programsC programs
C programsMinu S
 
Lab. Programs in C
Lab. Programs in CLab. Programs in C
Lab. Programs in CSaket Pathak
 
Chapter 3 : Balagurusamy Programming ANSI in C
Chapter 3 : Balagurusamy Programming ANSI in C Chapter 3 : Balagurusamy Programming ANSI in C
Chapter 3 : Balagurusamy Programming ANSI in C BUBT
 
Chapter 4 : Balagurusamy Programming ANSI in C
Chapter 4 : Balagurusamy Programming ANSI in CChapter 4 : Balagurusamy Programming ANSI in C
Chapter 4 : Balagurusamy Programming ANSI in CBUBT
 
Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solutionAzhar Javed
 
Programming fundamentals
Programming fundamentalsProgramming fundamentals
Programming fundamentalsZaibi Gondal
 
C - Programming Assignment 1 and 2
C - Programming Assignment 1 and 2C - Programming Assignment 1 and 2
C - Programming Assignment 1 and 2Animesh Chaturvedi
 
Functions & Procedures [7]
Functions & Procedures [7]Functions & Procedures [7]
Functions & Procedures [7]ecko_disasterz
 

What's hot (20)

C programming Lab 1
C programming Lab 1C programming Lab 1
C programming Lab 1
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given number
 
Pslb lab manual
Pslb lab manualPslb lab manual
Pslb lab manual
 
C Programming
C ProgrammingC Programming
C Programming
 
Lab manualsahu[et&amp;t]
Lab manualsahu[et&amp;t]Lab manualsahu[et&amp;t]
Lab manualsahu[et&amp;t]
 
C- Programming Assignment 4 solution
C- Programming Assignment 4 solutionC- Programming Assignment 4 solution
C- Programming Assignment 4 solution
 
Chapter 5 exercises Balagurusamy Programming ANSI in c
Chapter 5 exercises Balagurusamy Programming ANSI  in cChapter 5 exercises Balagurusamy Programming ANSI  in c
Chapter 5 exercises Balagurusamy Programming ANSI in c
 
C programming Lab 2
C programming Lab 2C programming Lab 2
C programming Lab 2
 
C- Programming Assignment 3
C- Programming Assignment 3C- Programming Assignment 3
C- Programming Assignment 3
 
Ansi c
Ansi cAnsi c
Ansi c
 
Chapter 5 Balagurusamy Programming ANSI in c
Chapter 5 Balagurusamy Programming ANSI  in cChapter 5 Balagurusamy Programming ANSI  in c
Chapter 5 Balagurusamy Programming ANSI in c
 
C important questions
C important questionsC important questions
C important questions
 
C programs
C programsC programs
C programs
 
Lab. Programs in C
Lab. Programs in CLab. Programs in C
Lab. Programs in C
 
Chapter 3 : Balagurusamy Programming ANSI in C
Chapter 3 : Balagurusamy Programming ANSI in C Chapter 3 : Balagurusamy Programming ANSI in C
Chapter 3 : Balagurusamy Programming ANSI in C
 
Chapter 4 : Balagurusamy Programming ANSI in C
Chapter 4 : Balagurusamy Programming ANSI in CChapter 4 : Balagurusamy Programming ANSI in C
Chapter 4 : Balagurusamy Programming ANSI in C
 
Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solution
 
Programming fundamentals
Programming fundamentalsProgramming fundamentals
Programming fundamentals
 
C - Programming Assignment 1 and 2
C - Programming Assignment 1 and 2C - Programming Assignment 1 and 2
C - Programming Assignment 1 and 2
 
Functions & Procedures [7]
Functions & Procedures [7]Functions & Procedures [7]
Functions & Procedures [7]
 

Viewers also liked (8)

week-2x
week-2xweek-2x
week-2x
 
week-4x
week-4xweek-4x
week-4x
 
week-5x
week-5xweek-5x
week-5x
 
C code examples
C code examplesC code examples
C code examples
 
week-1x
week-1xweek-1x
week-1x
 
ch14
ch14ch14
ch14
 
week-10x
week-10xweek-10x
week-10x
 
week-11x
week-11xweek-11x
week-11x
 

Similar to C Program to Perform Matrix Addition and Multiplication

C Programming lab
C Programming labC Programming lab
C Programming labVikram Nandini
 
Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020vrgokila
 
C lab manaual
C lab manaualC lab manaual
C lab manaualmanoj11manu
 
'C' language notes (a.p)
'C' language notes (a.p)'C' language notes (a.p)
'C' language notes (a.p)Ashishchinu
 
L25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxL25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxhappycocoman
 
12th CBSE Practical File
12th CBSE Practical File12th CBSE Practical File
12th CBSE Practical FileAshwin Francis
 
VTU Network lab programs
VTU Network lab   programsVTU Network lab   programs
VTU Network lab programsAnanda Kumar HN
 
SaraPIC
SaraPICSaraPIC
SaraPICSara Sahu
 
All important c programby makhan kumbhkar
All important c programby makhan kumbhkarAll important c programby makhan kumbhkar
All important c programby makhan kumbhkarsandeep kumbhkar
 
C basics
C basicsC basics
C basicsMSc CST
 
Data Structure using C
Data Structure using CData Structure using C
Data Structure using CBilal Mirza
 
Bcsl 033 data and file structures lab s1-1
Bcsl 033 data and file structures lab s1-1Bcsl 033 data and file structures lab s1-1
Bcsl 033 data and file structures lab s1-1Dr. Loganathan R
 

Similar to C Program to Perform Matrix Addition and Multiplication (20)

C lab programs
C lab programsC lab programs
C lab programs
 
C lab programs
C lab programsC lab programs
C lab programs
 
C programs
C programsC programs
C programs
 
C Programming lab
C Programming labC Programming lab
C Programming lab
 
Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020
 
C lab manaual
C lab manaualC lab manaual
C lab manaual
 
C PROGRAMS
C PROGRAMSC PROGRAMS
C PROGRAMS
 
'C' language notes (a.p)
'C' language notes (a.p)'C' language notes (a.p)
'C' language notes (a.p)
 
L25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxL25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptx
 
C-programs
C-programsC-programs
C-programs
 
C Programming Example
C Programming ExampleC Programming Example
C Programming Example
 
Cpds lab
Cpds labCpds lab
Cpds lab
 
12th CBSE Practical File
12th CBSE Practical File12th CBSE Practical File
12th CBSE Practical File
 
C file
C fileC file
C file
 
VTU Network lab programs
VTU Network lab   programsVTU Network lab   programs
VTU Network lab programs
 
SaraPIC
SaraPICSaraPIC
SaraPIC
 
All important c programby makhan kumbhkar
All important c programby makhan kumbhkarAll important c programby makhan kumbhkar
All important c programby makhan kumbhkar
 
C basics
C basicsC basics
C basics
 
Data Structure using C
Data Structure using CData Structure using C
Data Structure using C
 
Bcsl 033 data and file structures lab s1-1
Bcsl 033 data and file structures lab s1-1Bcsl 033 data and file structures lab s1-1
Bcsl 033 data and file structures lab s1-1
 

More from KITE www.kitecolleges.com (20)

DISTRIBUTED INTERACTIVE VIRTUAL ENVIRONMENT
DISTRIBUTED INTERACTIVE VIRTUAL ENVIRONMENTDISTRIBUTED INTERACTIVE VIRTUAL ENVIRONMENT
DISTRIBUTED INTERACTIVE VIRTUAL ENVIRONMENT
 
BrainFingerprintingpresentation
BrainFingerprintingpresentationBrainFingerprintingpresentation
BrainFingerprintingpresentation
 
ch6
ch6ch6
ch6
 
PPT (2)
PPT (2)PPT (2)
PPT (2)
 
week-18x
week-18xweek-18x
week-18x
 
ch16
ch16ch16
ch16
 
holographic versatile disc
holographic versatile discholographic versatile disc
holographic versatile disc
 
week-22x
week-22xweek-22x
week-22x
 
week-16x
week-16xweek-16x
week-16x
 
week-6x
week-6xweek-6x
week-6x
 
ch8
ch8ch8
ch8
 
Intro Expert Systems test-me.co.uk
Intro Expert Systems test-me.co.ukIntro Expert Systems test-me.co.uk
Intro Expert Systems test-me.co.uk
 
ch17
ch17ch17
ch17
 
ch4
ch4ch4
ch4
 
week-7x
week-7xweek-7x
week-7x
 
week-9x
week-9xweek-9x
week-9x
 
week-14x
week-14xweek-14x
week-14x
 
AIRBORNE
AIRBORNEAIRBORNE
AIRBORNE
 
ch2
ch2ch2
ch2
 
week-23x
week-23xweek-23x
week-23x
 

Recently uploaded

POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
MICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxMICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxabhijeetpadhi001
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)Dr. Mazin Mohamed alkathiri
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 

Recently uploaded (20)

POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
MICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxMICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptx
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 

C Program to Perform Matrix Addition and Multiplication

  • 1. /* 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");
  • 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*/ 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; }
  • 6. /* 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);
  • 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 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");
  • 9. 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;
  • 10. 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: