SlideShare a Scribd company logo
LAB EXERCISE 6 - 10
R SARASWATHI
SRI AKILANDESWARI WOMENS COLLEGE
Ex: 6. To Compute GCD and
LCM String Manipulation.
#include <stdio.h>
int gcd(int x, int y); //function prototype
int main()
{
int num1, num2, hcf, lcm;
printf("Enter two integer Values:n");
scanf("%d %d", &num1, &num2);
hcf = gcd(num1, num2);
printf("GCD: %d", hcf);
printf("nLCM: %d", (num1 * num2) / hcf);
return 0;
}
//recursive function
int gcd(int x, int y)
{
if (y == 0) //recursion termination condition
{
return x;
}
else
{
return gcd(y, x % y); //calls itself
}
}
Output
Enter two integer Values:
5
10
GCD: 5
LCM: 10
Ex:7 Operations on string such
as length, concatenation,
reverse, counting, and copy of a
string to another.
String Manipulation
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void main()
{ char str1[20],str2[20];
int ch,i,j,length,count=0;
do { printf("tMENU");
printf("n------------------------------n");
printf("1:Find Length of String");
printf("n2:Find Reverse of String");
printf("n3:Concatenate Strings");
printf("n4:Copy String ");
printf("n5:Vowels Counting");
printf("n6:Exit");
printf("n------------------------------n");
printf("nEnter your choice: ");
scanf("%d",&ch); switch(ch)
{
case 1:
printf("Enter String: ");
scanf("%s",str1);
i=strlen(str1);
printf("Length of String : %dnn",i);
break;
String Manipulation
case 2:
printf("Enter String: ");
scanf("%s",str1);
length=strlen(str1);
for(i = length - 1; i >= 0; i--)
{
printf("%c", str1[i]);
}
printf(" : Reverse stringn");
break;
case 3:
printf("nEnter First String: ");
scanf("%s",str1);
printf("Enter Second string: ");
scanf("%s",str2);
strcat(str1,str2);
printf("String After Concatenation : %snn",str1);
break;
String Manipulation
case 4:
printf("Enter a String1: ");
scanf("%s",str1);
printf("Enter a String2: ");
scanf("%s",str2);
printf("nString Before
Copied:nString1="%s",String2="%s"n",str1,str2);
strcpy(str2,str1);
printf("-----------------------------------------------n");
printf(""We are copying string String1 to String2" n");
printf("-----------------------------------------------n");
printf("String After
Copied:nString1="%s",String2="%s"nn",str1,str2);
break;
case 5:
printf("nEnter a String:[without space]t");
scanf("%s", str1);
length = strlen(str1);
for(i = 0; i < length; i++)
{
if(str1[i] == 'a' || str1[i] == 'e' || str1[i] == 'i' || str1[i] == 'o' || str1[i]
== 'u' || str1[i] == 'A' || str1[i] == 'E' || str1[i] == 'I'
|| str1[i] == 'O' || str1[i] == 'U')
count++;
}
printf("Total Vowels:%d n",count);
break;
case 6:
exit(0);
break;
default:
printf("Invalid Input. Please Enter valid Input.nn ");
} }
while(ch!=6);}
Output
MENU
------------------------------
1:Find Length of String
2:Find Reverse of String
3:Concatenate Strings
4:Copy String
5:Vowels Counting
6:Exit
------------------------------
Enter your choice: 1
Enter String: Structure
Length of String : 9
MENU
------------------------------
1:Find Length of String
2:Find Reverse of String
3:Concatenate Strings
4:Copy String
5:Vowels Counting
6:Exit
------------------------------
Enter your choice: 2
Enter String: union
noinu : Reverse string
MENU
------------------------------
1:Find Length of String
2:Find Reverse of String
3:Concatenate Strings
4:Copy String
5:Vowels Counting
6:Exit
------------------------------
Enter your choice: 3
Enter First String: Pointer
Enter Second string: Adree ss
String After Concatenation : PointerAdress
MENU
------------------------------
1:Find Length of String
2:Find Reverse of String
3:Concatenate Strings
4:Copy String
5:Vowels Counting
6:Exit
------------------------------
Enter your choice: 5
Enter a String:[without space] Variables
Total Vowels:4
MENU
------------------------------
1:Find Length of String
2:Find Reverse of String
3:Concatenate Strings
4:Copy String
5:Vowels Counting
6:Exit
------------------------------
Enter your choice: 6
Ex:8 Matrix Addition,
Subtraction, Multiplication,
Transpose of n x m matrices.
Matrix Manipulation
#include<stdio.h>
void main()
{
int r1,r2,c1,c2,i,j,k;
int m1[15][15],m2[15][15],a[15][15],s[15][15],m[15][15],t[15][15];
printf("ntt MATRIX MANIPULATION ");
printf("ntt ******************** ");
printf("n Enter the no.of rows and columns for matrix 1: ");
printf("n rows:");
scanf("%d",&r1);
printf("n columns:");
scanf("%d",&c1);
printf("Enter the values of martix 1: n");
for(i=1;i<=r1;i++)
for(j=1;j<=c1;j++)
scanf("%d",&m1[i][j]);
printf("n Enter the rows and columns of matrix 2: ");
printf("n rows:");
scanf("%d",&r2);
printf("columns:");
scanf("%d",&c2);
Matrix Manipulation
printf("n Enter the values of matrix 2: n");
for(i=1;i<=r2;i++)
for(j=1;j<=c2;j++)
scanf("%d",&m2[i][j]);
printf("n MATRIX ADDITION ");
printf("n --------------- n");
if(r1==r2&&c1==c2)
{
for(i=1;i<=r1;i++)
{
for(j=1;j<=c1;j++)
{
a[i][j]=m1[i][j]+m2[i][j];
printf("t%d",a[i][j]);
}
printf("n");
}}
else
printf("n matrix addition is not possible");
printf("n MATRIX SUBTRACTION");
printf("n ------------------ n");
Matrix Manipulation
if(r1==r2&&c1==c2)
{ for(i=1;i<=r1;i++)
{ for(j=1;j<=c1;j++)
{
s[i][j]=m1[i][j]-m2[i][j];
printf("t%d",s[i][j]);
}
printf("n");
}}
else
printf("nmatrix subtraction is not possible");
printf("n MATRIX MULTIPLICATIONS");
printf("n ---------------------- n");
if(r2==c1)
{
for(i=1;i<=r1;i++)
{
printf("n");
for(j=1;j<=c2;j++)
{
m[i][j]=0;
for(k=1;k<=r2;k++)
m[i][j]=m[i][j]+(m1[i][k]*m2[k][j]);
}}}
Matrix Manipulation
else
printf("n multiplication is not possible");
for(i=1;i<=r1;i++)
{
for(j=1;j<=c2;j++)
printf("t%d",m[i][j]);
printf("n");
}
printf("nTRANSPOSE OF MATRIXn");
printf("n ---------------------- n");
for(i=1;i<=r1;i++)
{
for(j=1;j<=c1;j++)
t[i][j]=m1[j][i];
}
for(i=1;i<=c1;i++)
{
for(j=1;j<=r1;j++)
printf("t %d",t[i][j]);
printf("n");
}
getch();
}
Output
MATRIX MANIPULATION
********************
Enter the no.of rows and columns for matrix 1:
rows:3
columns:3
Enter the values of martix 1:
2 4 6 8 10 12 14 16 18
Enter the rows and columns of matrix 2:
rows:3
columns:3
Enter the values of matrix 2:
3 6 9 12 15 18 21 24 27
MATRIX ADDITION
--------------------------
5 10 15
20 25 30
35 40 45
MATRIX SUBTRACTION
------------------------------
-1 -2 -3
-4 -5 -6
-7 -8 -9
MATRIX MULTIPLICATIONS
--------------------------------
180 216 252
396 486 576
612 756 900
TRANSPOSE OF MATRIX
2 8 14
4 10 16
6 12 18
Ex: 9. Inverse of a square
matrix.
#include<stdio.h>
int main(){
int a[3][3],i,j;
float determinant=0;
printf("Enter the 9 elements of matrix[Transpose of 3X3 Matrix]: ");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
printf("nThe matrix isn");
for(i=0;i<3;i++){
printf("n");
for(j=0;j<3;j++)
printf("%dt",a[i][j]);
}
for(i=0;i<3;i++)
determinant = determinant + (a[0][i]*(a[1][(i+1)%3]*a[2][(i+2)%3] - a[1][(i+2)%3]*a[2][(i+1)%3]));
printf("nInverse of matrix is: nn");
for(i=0;i<3;i++){
for(j=0;j<3;j++)
printf("%.2ft",((a[(i+1)%3][(j+1)%3] * a[(i+2)%3][(j+2)%3]) - (a[(i+1)%3][(j+2)%3]*a[(i+2)%3][(j+1)%3]))/
determinant);
printf("nInverse of matrix is: nn");
for(i=0;i<3;i++){
for(j=0;j<3;j++)
printf("%.2ft",((a[(i+1)%3][(j+1)%3]
* a[(i+2)%3][(j+2)%3]) -
(a[(i+1)%3][(j+2)%3]*a[(i+2)%3][(j+1)%3]))/
determinant);
printf("n");
}
return 0;
}
Output
Enter the 9 elements of matrix: 3
5
2
1
5
8
3
9
2
The matrix is
3 5 2
1 5 8
3 9 2
Inverse of matrix is:
0.70 -0.25 0.07
-0.09 -0.00 0.14
-0.34 0.25 -0.11
Ex: 10. Binary Search
#include <stdio.h>
int main()
{
int c, first, last, middle, n, search, array[100];
printf("Enter number of elementsn");
scanf("%d", &n);
printf("Enter %d unique integersn", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter value to findn");
scanf("%d", &search);
first = 0;
last = n - 1;
middle = (first+last)/2;
while (first <= last) {
if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search) {
printf("%d found at location
%d.n", search, middle+1);
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if (first > last)
printf("Not found! %d isn't present in the
list.n", search);
return 0;
}
Output
Enter number of elements
5
Enter 5 unique integers
8
6
4
3
2
Enter value to find
4
found at location 3
Enter number of elements
5
Enter 5 integers
2
3
4
5
6
Enter value to find
8
Not found! 8 isn't present in the list

More Related Content

DOC
Ramco C Question Paper 2003
PDF
How To Crack RSA Netrek Binary Verification System
PDF
NUMERICAL METHODS WITH MATLAB : bisection,mueller's,newton-raphson,false poin...
PDF
CRL 1.8 Functions
PDF
CRL 1.8 functions MrG 2011.0920 - sage
PPTX
Block diagram, Transfer Function from block diagram reduction, (8 Rules to re...
PDF
Numerical Algorithm for a few Special Functions
PDF
Let’s talk about microbenchmarking
Ramco C Question Paper 2003
How To Crack RSA Netrek Binary Verification System
NUMERICAL METHODS WITH MATLAB : bisection,mueller's,newton-raphson,false poin...
CRL 1.8 Functions
CRL 1.8 functions MrG 2011.0920 - sage
Block diagram, Transfer Function from block diagram reduction, (8 Rules to re...
Numerical Algorithm for a few Special Functions
Let’s talk about microbenchmarking

What's hot (20)

PDF
Homework 1 of Optical Semiconductor
PDF
Matlab file
PPT
Week 10 part 1 pe 6282 Block Diagrams
PDF
CM1.4 Algorithms, python and SAGE
PDF
AST: threats and opportunities
TXT
Script clases ide bd espacial
PPTX
Sketching derivatives
TXT
Snake.c
PDF
Graph Algebra
PDF
Understanding the nodejs event loop
PDF
SPSF03 - Numerical Integrations
PDF
SPSF02 - Graphical Data Representation
DOCX
Recursion in C
PDF
Lecture 2 f17
PDF
MLE Example
PDF
SPSF04 - Euler and Runge-Kutta Methods
PPT
5.4 Slope Intercept Form Part B
PDF
Descriptive analytics in r programming language
PPT
Postfix Evaluations using Stack
PDF
Save all the modules
Homework 1 of Optical Semiconductor
Matlab file
Week 10 part 1 pe 6282 Block Diagrams
CM1.4 Algorithms, python and SAGE
AST: threats and opportunities
Script clases ide bd espacial
Sketching derivatives
Snake.c
Graph Algebra
Understanding the nodejs event loop
SPSF03 - Numerical Integrations
SPSF02 - Graphical Data Representation
Recursion in C
Lecture 2 f17
MLE Example
SPSF04 - Euler and Runge-Kutta Methods
5.4 Slope Intercept Form Part B
Descriptive analytics in r programming language
Postfix Evaluations using Stack
Save all the modules
Ad

Similar to C PROGRAMS - SARASWATHI RAMALINGAM (20)

DOC
C lab-programs
PPTX
SIMPLE C PROGRAMS - SARASWATHI RAMALINGAM
PPTX
Best C++ Programming Homework Help
PDF
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
PPTX
Tharun prakash.pptx
PPTX
Programming Assignment Help
PDF
C Programming lab
DOC
Basic c programs updated on 31.8.2020
PDF
C Programming Interview Questions
DOCX
C_Lab Manual_Part A.docx
DOCX
PDF
Cpd lecture im 207
PDF
PPTX
UNIT 4C-Strings.pptx for c language and basic knowledge
DOCX
12th CBSE Practical File
PDF
The best every notes on c language is here check it out
C lab-programs
SIMPLE C PROGRAMS - SARASWATHI RAMALINGAM
Best C++ Programming Homework Help
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
Tharun prakash.pptx
Programming Assignment Help
C Programming lab
Basic c programs updated on 31.8.2020
C Programming Interview Questions
C_Lab Manual_Part A.docx
Cpd lecture im 207
UNIT 4C-Strings.pptx for c language and basic knowledge
12th CBSE Practical File
The best every notes on c language is here check it out
Ad

More from SaraswathiRamalingam (20)

PPTX
PPTX
XSL - XML STYLE SHEET
PPTX
PPTX
PPTX
XML DTD DOCUMENT TYPE DEFINITION
PPTX
Georg scheutz - Charles babbage - Saraswathi Ramalingam
PPTX
Dennis ritchie - SARASWATHI RAMALINGAM
PPTX
Arithmetic expression INFIX TO POSTFIX CONVERTION saraswathi ramalingam
PPTX
PROGRAMMING USING C# .NET - SARASWATHI RAMALINGAM
PPTX
PROGRAMMING USING C# .NET - SARASWATHI RAMALINGAM
PPTX
PROGRAMMING USING C# .NET - SARASWATHI RAMALINGAM
PPTX
PROGRAMMING USING C# .NET - SARASWATHI RAMALINGAM
PPTX
PROGRAMMING USING C# .NET - SARASWATHI RAMALINGAM
PPTX
PROGRAMMING USING C# .NET - SARASWATHI RAMALINGAM
PPTX
PROGRAMMING USING C# .NET - SARASWATHI RAMALINGAM
PPTX
PROGRAMMING USING C# .NET - SARASWATHI RAMALINGAM
PPTX
LAB PROGRAMS SARASWATHI RAMALINGAM
XSL - XML STYLE SHEET
XML DTD DOCUMENT TYPE DEFINITION
Georg scheutz - Charles babbage - Saraswathi Ramalingam
Dennis ritchie - SARASWATHI RAMALINGAM
Arithmetic expression INFIX TO POSTFIX CONVERTION saraswathi ramalingam
PROGRAMMING USING C# .NET - SARASWATHI RAMALINGAM
PROGRAMMING USING C# .NET - SARASWATHI RAMALINGAM
PROGRAMMING USING C# .NET - SARASWATHI RAMALINGAM
PROGRAMMING USING C# .NET - SARASWATHI RAMALINGAM
PROGRAMMING USING C# .NET - SARASWATHI RAMALINGAM
PROGRAMMING USING C# .NET - SARASWATHI RAMALINGAM
PROGRAMMING USING C# .NET - SARASWATHI RAMALINGAM
PROGRAMMING USING C# .NET - SARASWATHI RAMALINGAM
LAB PROGRAMS SARASWATHI RAMALINGAM

Recently uploaded (20)

PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Complications of Minimal Access Surgery at WLH
PDF
01-Introduction-to-Information-Management.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
TR - Agricultural Crops Production NC III.pdf
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
RMMM.pdf make it easy to upload and study
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
VCE English Exam - Section C Student Revision Booklet
FourierSeries-QuestionsWithAnswers(Part-A).pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Complications of Minimal Access Surgery at WLH
01-Introduction-to-Information-Management.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
Final Presentation General Medicine 03-08-2024.pptx
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
Supply Chain Operations Speaking Notes -ICLT Program
Anesthesia in Laparoscopic Surgery in India
TR - Agricultural Crops Production NC III.pdf
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
O7-L3 Supply Chain Operations - ICLT Program
RMMM.pdf make it easy to upload and study
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPH.pptx obstetrics and gynecology in nursing
VCE English Exam - Section C Student Revision Booklet

C PROGRAMS - SARASWATHI RAMALINGAM

  • 1. LAB EXERCISE 6 - 10 R SARASWATHI SRI AKILANDESWARI WOMENS COLLEGE
  • 2. Ex: 6. To Compute GCD and LCM String Manipulation. #include <stdio.h> int gcd(int x, int y); //function prototype int main() { int num1, num2, hcf, lcm; printf("Enter two integer Values:n"); scanf("%d %d", &num1, &num2); hcf = gcd(num1, num2); printf("GCD: %d", hcf); printf("nLCM: %d", (num1 * num2) / hcf); return 0; } //recursive function int gcd(int x, int y) { if (y == 0) //recursion termination condition { return x; } else { return gcd(y, x % y); //calls itself } }
  • 3. Output Enter two integer Values: 5 10 GCD: 5 LCM: 10
  • 4. Ex:7 Operations on string such as length, concatenation, reverse, counting, and copy of a string to another.
  • 5. String Manipulation #include<stdio.h> #include<string.h> #include<stdlib.h> void main() { char str1[20],str2[20]; int ch,i,j,length,count=0; do { printf("tMENU"); printf("n------------------------------n"); printf("1:Find Length of String"); printf("n2:Find Reverse of String"); printf("n3:Concatenate Strings"); printf("n4:Copy String "); printf("n5:Vowels Counting"); printf("n6:Exit"); printf("n------------------------------n"); printf("nEnter your choice: "); scanf("%d",&ch); switch(ch) { case 1: printf("Enter String: "); scanf("%s",str1); i=strlen(str1); printf("Length of String : %dnn",i); break;
  • 6. String Manipulation case 2: printf("Enter String: "); scanf("%s",str1); length=strlen(str1); for(i = length - 1; i >= 0; i--) { printf("%c", str1[i]); } printf(" : Reverse stringn"); break; case 3: printf("nEnter First String: "); scanf("%s",str1); printf("Enter Second string: "); scanf("%s",str2); strcat(str1,str2); printf("String After Concatenation : %snn",str1); break;
  • 7. String Manipulation case 4: printf("Enter a String1: "); scanf("%s",str1); printf("Enter a String2: "); scanf("%s",str2); printf("nString Before Copied:nString1="%s",String2="%s"n",str1,str2); strcpy(str2,str1); printf("-----------------------------------------------n"); printf(""We are copying string String1 to String2" n"); printf("-----------------------------------------------n"); printf("String After Copied:nString1="%s",String2="%s"nn",str1,str2); break;
  • 8. case 5: printf("nEnter a String:[without space]t"); scanf("%s", str1); length = strlen(str1); for(i = 0; i < length; i++) { if(str1[i] == 'a' || str1[i] == 'e' || str1[i] == 'i' || str1[i] == 'o' || str1[i] == 'u' || str1[i] == 'A' || str1[i] == 'E' || str1[i] == 'I' || str1[i] == 'O' || str1[i] == 'U') count++; } printf("Total Vowels:%d n",count); break; case 6: exit(0); break; default: printf("Invalid Input. Please Enter valid Input.nn "); } } while(ch!=6);}
  • 9. Output MENU ------------------------------ 1:Find Length of String 2:Find Reverse of String 3:Concatenate Strings 4:Copy String 5:Vowels Counting 6:Exit ------------------------------ Enter your choice: 1 Enter String: Structure Length of String : 9
  • 10. MENU ------------------------------ 1:Find Length of String 2:Find Reverse of String 3:Concatenate Strings 4:Copy String 5:Vowels Counting 6:Exit ------------------------------ Enter your choice: 2 Enter String: union noinu : Reverse string MENU ------------------------------ 1:Find Length of String 2:Find Reverse of String 3:Concatenate Strings 4:Copy String 5:Vowels Counting 6:Exit ------------------------------ Enter your choice: 3 Enter First String: Pointer Enter Second string: Adree ss String After Concatenation : PointerAdress
  • 11. MENU ------------------------------ 1:Find Length of String 2:Find Reverse of String 3:Concatenate Strings 4:Copy String 5:Vowels Counting 6:Exit ------------------------------ Enter your choice: 5 Enter a String:[without space] Variables Total Vowels:4 MENU ------------------------------ 1:Find Length of String 2:Find Reverse of String 3:Concatenate Strings 4:Copy String 5:Vowels Counting 6:Exit ------------------------------ Enter your choice: 6
  • 12. Ex:8 Matrix Addition, Subtraction, Multiplication, Transpose of n x m matrices.
  • 13. Matrix Manipulation #include<stdio.h> void main() { int r1,r2,c1,c2,i,j,k; int m1[15][15],m2[15][15],a[15][15],s[15][15],m[15][15],t[15][15]; printf("ntt MATRIX MANIPULATION "); printf("ntt ******************** "); printf("n Enter the no.of rows and columns for matrix 1: "); printf("n rows:"); scanf("%d",&r1); printf("n columns:"); scanf("%d",&c1); printf("Enter the values of martix 1: n"); for(i=1;i<=r1;i++) for(j=1;j<=c1;j++) scanf("%d",&m1[i][j]); printf("n Enter the rows and columns of matrix 2: "); printf("n rows:"); scanf("%d",&r2); printf("columns:"); scanf("%d",&c2);
  • 14. Matrix Manipulation printf("n Enter the values of matrix 2: n"); for(i=1;i<=r2;i++) for(j=1;j<=c2;j++) scanf("%d",&m2[i][j]); printf("n MATRIX ADDITION "); printf("n --------------- n"); if(r1==r2&&c1==c2) { for(i=1;i<=r1;i++) { for(j=1;j<=c1;j++) { a[i][j]=m1[i][j]+m2[i][j]; printf("t%d",a[i][j]); } printf("n"); }} else printf("n matrix addition is not possible"); printf("n MATRIX SUBTRACTION"); printf("n ------------------ n");
  • 15. Matrix Manipulation if(r1==r2&&c1==c2) { for(i=1;i<=r1;i++) { for(j=1;j<=c1;j++) { s[i][j]=m1[i][j]-m2[i][j]; printf("t%d",s[i][j]); } printf("n"); }} else printf("nmatrix subtraction is not possible"); printf("n MATRIX MULTIPLICATIONS"); printf("n ---------------------- n"); if(r2==c1) { for(i=1;i<=r1;i++) { printf("n"); for(j=1;j<=c2;j++) { m[i][j]=0; for(k=1;k<=r2;k++) m[i][j]=m[i][j]+(m1[i][k]*m2[k][j]); }}}
  • 16. Matrix Manipulation else printf("n multiplication is not possible"); for(i=1;i<=r1;i++) { for(j=1;j<=c2;j++) printf("t%d",m[i][j]); printf("n"); } printf("nTRANSPOSE OF MATRIXn"); printf("n ---------------------- n"); for(i=1;i<=r1;i++) { for(j=1;j<=c1;j++) t[i][j]=m1[j][i]; } for(i=1;i<=c1;i++) { for(j=1;j<=r1;j++) printf("t %d",t[i][j]); printf("n"); } getch(); }
  • 17. Output MATRIX MANIPULATION ******************** Enter the no.of rows and columns for matrix 1: rows:3 columns:3 Enter the values of martix 1: 2 4 6 8 10 12 14 16 18 Enter the rows and columns of matrix 2: rows:3 columns:3 Enter the values of matrix 2: 3 6 9 12 15 18 21 24 27
  • 18. MATRIX ADDITION -------------------------- 5 10 15 20 25 30 35 40 45 MATRIX SUBTRACTION ------------------------------ -1 -2 -3 -4 -5 -6 -7 -8 -9 MATRIX MULTIPLICATIONS -------------------------------- 180 216 252 396 486 576 612 756 900 TRANSPOSE OF MATRIX 2 8 14 4 10 16 6 12 18
  • 19. Ex: 9. Inverse of a square matrix. #include<stdio.h> int main(){ int a[3][3],i,j; float determinant=0; printf("Enter the 9 elements of matrix[Transpose of 3X3 Matrix]: "); for(i=0;i<3;i++) for(j=0;j<3;j++) scanf("%d",&a[i][j]); printf("nThe matrix isn"); for(i=0;i<3;i++){ printf("n"); for(j=0;j<3;j++) printf("%dt",a[i][j]); } for(i=0;i<3;i++) determinant = determinant + (a[0][i]*(a[1][(i+1)%3]*a[2][(i+2)%3] - a[1][(i+2)%3]*a[2][(i+1)%3])); printf("nInverse of matrix is: nn"); for(i=0;i<3;i++){ for(j=0;j<3;j++) printf("%.2ft",((a[(i+1)%3][(j+1)%3] * a[(i+2)%3][(j+2)%3]) - (a[(i+1)%3][(j+2)%3]*a[(i+2)%3][(j+1)%3]))/ determinant);
  • 20. printf("nInverse of matrix is: nn"); for(i=0;i<3;i++){ for(j=0;j<3;j++) printf("%.2ft",((a[(i+1)%3][(j+1)%3] * a[(i+2)%3][(j+2)%3]) - (a[(i+1)%3][(j+2)%3]*a[(i+2)%3][(j+1)%3]))/ determinant); printf("n"); } return 0; }
  • 21. Output Enter the 9 elements of matrix: 3 5 2 1 5 8 3 9 2 The matrix is 3 5 2 1 5 8 3 9 2 Inverse of matrix is: 0.70 -0.25 0.07 -0.09 -0.00 0.14 -0.34 0.25 -0.11
  • 22. Ex: 10. Binary Search #include <stdio.h> int main() { int c, first, last, middle, n, search, array[100]; printf("Enter number of elementsn"); scanf("%d", &n); printf("Enter %d unique integersn", n); for (c = 0; c < n; c++) scanf("%d", &array[c]); printf("Enter value to findn"); scanf("%d", &search); first = 0; last = n - 1; middle = (first+last)/2; while (first <= last) {
  • 23. if (array[middle] < search) first = middle + 1; else if (array[middle] == search) { printf("%d found at location %d.n", search, middle+1); break; } else last = middle - 1; middle = (first + last)/2; } if (first > last) printf("Not found! %d isn't present in the list.n", search); return 0; }
  • 24. Output Enter number of elements 5 Enter 5 unique integers 8 6 4 3 2 Enter value to find 4 found at location 3 Enter number of elements 5 Enter 5 integers 2 3 4 5 6 Enter value to find 8 Not found! 8 isn't present in the list