Lab No.


Name: YOGESH PARTH Dept: Avionics

Roll No. SC11B128 Sub: C Programming Lab

Question 11. Matrix Manipulation: Write a C program to find sum, product, trace of given two 22 matrices. Each function should be written in separate file. use appropriate inclusion directive to combine all the files to create one main file. use separate file to read the data from user and to display the results to user. Program code: Sum: #include<stdio.h> void sum(int a[2][2], int b[2][2], int c[2][2]){ int i=0, j=0; printf("Sum of Matrices:\n"); for(i=0;i<=1;i++){ for(j=0;j<=1;j++){ c[i][j]=a[i][j]+b[i][j]; printf("sum[%d][%d]= %d\n",i,j,c[i][j]); } } } Product: #include<conio.h> #include<stdio.h> void prod(int a[2][2], int b[2][2], int c[2][2]){ int i=0, j=0,m=0; printf("Product of the Matrices:\n"); for(i=0;i<=1;i++){ for(j=0;j<=1;j++){ c[i][j]=0; for(m=0;m<=1;m++){ c[i][j]+=a[i][m]*b[m][j]; } printf("prd[%d][%d]= %d\n",i,j,c[i][j]); } } } Sum column: #include<stdio.h> void sum_col(int a[2][2],int c[1][2]){ int i=0,j=0;

} Main: #include"sum.i<=1.cpp" #include"trace.j++){ c[i][0]+=a[i][j].j=0. c[0][0]=0.j<[2][1].i.h> void trace(int a[2][2]){ int tr=0.h> void sum_row(int a[2][2]. .h> #include<stdio.s[2][2].j++){ if(i==j) tr+=a[i][j].j<=1.j<=1.i<=1.b[2][2]. int i=0.cpp" #include"prod. for(i=0.p[2][2]. } } Sum row: #include<stdio. } printf("sumofrow[%d][0]= %d\n". for(i=0.cpp" #include"sum_col. } printf("sumofcol[0][%d]= %d\n".i++){ for(j=0.cpp" #include<conio.i++){ for(j=0. int<=1.i++){ for(j=0.c[i][0]).int c[2][1]){ int[1][2]. } } printf("trace of matrix= %d\n".i. for(i=0.cpp" #include"sum_row.h> main(){ int a[2][2].j++){ c[0][i]+=a[j][i].j=0. c[0][1]=0. c[1][0]=0. } } Trace: #include<stdio.c[0][i]).j=0.c[0][0]=0.

getch(). trace(a). int n.b.n).sc). sum(a.s. else printf("\n%d is odd".p).j<=1. sum(a.j++){ printf("Enter a[%d][%d]:".sr).i.j++){ printf("Enter b[%d][%d]:".s). } Result: Discussion: Question 2: Program to print the numbers between 1 and 10.j). along with an indication of whether each is even or odd. for(n=1.n++) { if(n%2==0) printf("\n%d is even".h> void main() { clrscr().i++){ for(j=0. } getch(). Program code: #include<stdio.j).n<=10.i<=1. scanf("%d". sum_col(a.i++){ for(j=0. } Result: .&a[i][j]).h> #include<conio.b.&b[i][j]). scanf("%d".j<=1. } } prod(a.for(i=0.n). sum_row(a.i<=1.s).i. } } for(i=0.

is odd or even. } return 0.h> #include<conio. i = i + 1) { factorial = factorial * i. printf("%d %d\n". . Question 3: Program to print the first 7 positive integers and their factorials. } Result: 1 1 2 2 3 6 4 24 5 120 6 720 7 5040 Press any key to continue . . factorial). for(i = 1.1 is odd 2 is even 3 is odd 4 is even 5 is odd 6 is even 7 is odd 8 is even 9 is odd 10 is even Discussion: We are using ‘for’ and ‘if-else’ command along with basic division by 2 to determine whether no. i.h> int main() { int i. int factorial = 1. Program code: #include <stdio. . i <= 7.

h> #include<conio. 2. printf("%d.x=0. x=y. 3. 3. int i. printf("%d. 144.z). 1. Question 4: Program to print the first 10 Fibonacci numbers. The sequence starts out 0. Program code: #include<stdio.i<=10.y=1. } Result: The first 10 Fibonacci numbers: 0. 1. 13. Each Fibonacci number is the sum of the two preceding ones.h> void main() { clrscr().". y=z. } getch().".Discussion: ‘For’ looping statement is being used to calculate the factorial. 1. 5. 8.i++) { z=x+y. (b) A B B A A B .. Question 5: Write a program to print the following pattern: Ques. 1. .z..y). (a) Ques. 89.%d. for(i=0. 2. Discussion: ‘For’ Looping statement is used to print the first 10 Fibonacci numbers. 5. 21.x. 8. 55. 34.

j. } printf("\n"). (a): #include<stdio. printf(" %c".C D E F Program code: For ques. printf(" %c". int i. for(j=1.p).h> #include<conio.i++) { for(j=1.j++) { p=j+64.j<=i. (b): #include<stdio.j++) { printf(" %c".h> #include<conio. for (i=1.h> void main() { clrscr().i<=6.p). } getch(). for (i=1. } Result: C D E F C D D E E E F F F F A A A A B B B B C C D C D E C D E F . char c.p). } getch().j<=i. } printf("\n").i<=6.j. } For ques.p.p.h> void main() { clrscr().i++) { p=i+64. int i.

W. } . H. V. O. } printf("\n").b. Q Program code: #include<stdio. int a. Y. T.b<=5. Question 6(Bonus): Write a program to display any one letter out of the following letters in a grid of 15 rows and 18 columns of stars (*).h> void main() { clrscr(). A. Z.a++) { if(a<=11) { for(b=1. L.a<=15.Output 1: A B C D E F B C D E F C D D E E E F F F F Output 2: A A A A A A B B B B B C C D C D E C D E F Discussion: We are using ‘for’ looping statement along with the fact that capital letter lie within number 65 to 90.b++) { printf("*"). U. S.h> #include<conio. C. for(a=1.

} Result: ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ****************** ****************** ****************** ******************* Discussion: We are using ‘for’ looping statement along with ‘if-else’ statement to design a pattern of L. } } getch().b<=18.else { for(b=1. } printf("\n"). .b++) { printf("*").

Sign up to vote on this title
UsefulNot useful