0% found this document useful (0 votes)
0 views

CHAR STRING EXAMPLE PROGRAM

The document contains multiple C programming examples demonstrating various operations such as array manipulation, matrix addition, subtraction, multiplication, and properties like symmetry and identity. Each example includes code snippets along with their expected output and explanations of the logic used. The document serves as a tutorial for basic programming concepts in C, particularly focusing on arrays and matrices.

Uploaded by

yesudossj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

CHAR STRING EXAMPLE PROGRAM

The document contains multiple C programming examples demonstrating various operations such as array manipulation, matrix addition, subtraction, multiplication, and properties like symmetry and identity. Each example includes code snippets along with their expected output and explanations of the logic used. The document serves as a tutorial for basic programming concepts in C, particularly focusing on arrays and matrices.

Uploaded by

yesudossj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

1. #include <stdio.

h>
int main() {
char letters[5] = {'A', 'B', 'C', 'D', 'E'};
printf("Characters in array:\n");
for(int i = 0; i < 5; i++) {
printf("%c ", letters[i]);
}
return 0;
}
OUTPUT
Characters in array:
ABCDE

2. #include <stdio.h>
int main() {
char name[20];
printf("Enter your name: ");
scanf("%s", name);
printf("Hello, %s!\n", name);
return 0;
}
OUTPUT:
Enter your name: YESUDOSS
Hello, YESUDOSS !
3. #include <stdio.h>
int main() {
float marks[5] = {87.5, 90.0, 76.5, 82.0, 88.5};
printf("Float values in array:\n");
for(int i = 0; i < 5; i++) {
printf("%.2f ", marks[i]);
}
return 0;
}
OUTPUT
Float values in array:
87.50 90.00 76.50 82.00 88.50

4. #include <stdio.h>
int main() {
char letters[2][3] = {
{'A', 'B', 'C'},
{'D', 'E', 'F'}
};
printf("2D Char Array:\n");
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 3; j++) {
printf("%c ", letters[i][j]);
}
printf("\n");
}
return 0;
}
OUTPUT
2D Char Array:
ABC
DEF
5. #include <stdio.h>
int main() {
char names[3][20] = {
"Alice",
"Bob",
"Charlie"
};
printf("Names in array:\n");
for(int i = 0; i < 3; i++) {
printf("%s\n", names[i]);
}
return 0;
}
OUTPUT:
Names in array:
Alice
Bob
Charlie
EXAMPLE PROGRAMS

✅ 1. Matrix Addition

#include <stdio.h>

int main() {
int A[2][2] = {{1, 2}, {3, 4}};
int B[2][2] = {{5, 6}, {7, 8}};
int C[2][2];

printf("Matrix Addition:\n");
for(int i=0;i<2;i++) {
for(int j=0;j<2;j++) {
C[i][j] = A[i][j] + B[i][j];
printf("%d ", C[i][j]);
}
printf("\n");
}
return 0;
}

Output
Matrix Addition:
68
10 12

Explanation
for(int i=0; i<2; i++)

 Outer loop: Goes through rows (0 and 1).


🔁 for(int j=0; j<2; j++)

 Inner loop: Goes through columns (0 and 1).

➕ C[i][j] = A[i][j] + B[i][j];

 Adds elements from A and B and stores in C.

For example:

 C[0][0] = 1 + 5 = 6
 C[0][1] = 2 + 6 = 8
 C[1][0] = 3 + 7 = 10
 C[1][1] = 4 + 8 = 12

🖨️printf("%d ", C[i][j]);

 Prints each element of matrix C row by row.

printf("\n");

 Moves to the next line after printing one row.

✅ 2. Matrix Subtraction

#include <stdio.h>

int main() {
int A[2][2] = {{5, 6}, {7, 8}};
int B[2][2] = {{1, 2}, {3, 4}};
int C[2][2];

printf("Matrix Subtraction:\n");
for(int i=0;i<2;i++) {
for(int j=0;j<2;j++) {
C[i][j] = A[i][j] - B[i][j];
printf("%d ", C[i][j]);
}
printf("\n");
}
return 0;
}

✅ 3. Matrix Multiplication

#include <stdio.h>

int main() {
int A[2][2] = {{1, 2}, {3, 4}};
int B[2][2] = {{5, 6}, {7, 8}};
int C[2][2] = {0};

printf("Matrix Multiplication:\n");
for(int i=0;i<2;i++) {
for(int j=0;j<2;j++) {
for(int k=0;k<2;k++) {
C[i][j] += A[i][k] * B[k][j];
}
printf("%d ", C[i][j]);
}
printf("\n");
}
return 0;
}

✅ 4. Transpose of a Matrix

#include <stdio.h>

int main() {
int A[2][3] = {{1, 2, 3}, {4, 5, 6}};
int T[3][2];

printf("Transpose:\n");
for(int i=0;i<2;i++) {
for(int j=0;j<3;j++) {
T[j][i] = A[i][j];
}
}

for(int i=0;i<3;i++) {
for(int j=0;j<2;j++) {
printf("%d ", T[i][j]);
}
printf("\n");
}

return 0;
}

✅ 5. Check Symmetric Matrix

#include <stdio.h>

int main() {
int A[2][2] = {{1, 2}, {2, 1}};
int flag = 1;

for(int i=0;i<2;i++) {
for(int j=0;j<2;j++) {
if(A[i][j] != A[j][i]) {
flag = 0;
break;
}
}
}

if(flag)
printf("Matrix is symmetric\n");
else
printf("Matrix is not symmetric\n");
return 0;
}

✅ 6. Diagonal Elements

#include <stdio.h>

int main() {
int A[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

printf("Diagonal Elements:\n");
for(int i=0;i<3;i++) {
printf("%d ", A[i][i]);
}

return 0;
}

✅ 7. Identity Matrix

#include <stdio.h>

int main() {
int I[3][3];

printf("Identity Matrix:\n");
for(int i=0;i<3;i++) {
for(int j=0;j<3;j++) {
if(i == j)
I[i][j] = 1;
else
I[i][j] = 0;
printf("%d ", I[i][j]);
}
printf("\n");
}
return 0;
}

✅ 8. Upper & Lower Triangular Matrix

#include <stdio.h>

int main() {
int A[3][3] = {{1, 2, 3}, {0, 5, 6}, {0, 0, 9}};

printf("Upper Triangular Matrix:\n");


for(int i=0;i<3;i++) {
for(int j=0;j<3;j++) {
if(i > j)
printf("0 ");
else
printf("%d ", A[i][j]);
}
printf("\n");
}

printf("\nLower Triangular Matrix:\n");


for(int i=0;i<3;i++) {
for(int j=0;j<3;j++) {
if(i < j)
printf("0 ");
else
printf("%d ", A[i][j]);
}
printf("\n");
}

return 0;
}

✅ 9. Sparse Matrix
#include <stdio.h>

int main() {
int A[3][3] = {{0, 0, 3}, {0, 0, 0}, {4, 0, 0}};
int zero = 0, total = 3*3;

for(int i=0;i<3;i++) {
for(int j=0;j<3;j++) {
if(A[i][j] == 0)
zero++;
}
}

if(zero > total / 2)


printf("It is a sparse matrix\n");
else
printf("It is not a sparse matrix\n");

return 0;
}

✅ 10. Determinant of a 2x2 Matrix

#include <stdio.h>

int main() {
int a[2][2] = {{4, 3}, {6, 3}};
int det;

det = (a[0][0] * a[1][1]) - (a[0][1] * a[1][0]);

printf("Determinant: %d\n", det);

return 0;
}

You might also like